MATLAB: How to curve fit to find a value

curve fittingequation solving

I have the equation set
w = sqrt((1/(L.*C))-(R./(2.*L))^2);
I = (V./(w.*L)).*exp(-(R./(2.*L)).*t).*sin(w.*t);
I have a data set for I and t and known values for R and C and V, how do i curve fit using the equations to find L

Best Answer

One approach:
R = 0.6;
C = 270E-12;
V = 100E3;
w = @(L) sqrt((1/(L.*C))-(R./(2.*L))^2);
objfcn = @(L,t) (V./(w(L).*L)).*exp(-(R./(2.*L)).*t).*sin(w(L).*t);
I = rand(1,10); % Create Data

t = 0:9; % Create Data
[L,resnrm] = fminsearch(@(L) norm(I - objfcn(L,t)), 42)
Use your own vectors for ‘I’ and ‘t’. This simply shows how to use them with the functions and fminsearch.
EDIT — (3 Nov 2020 at 18:26)
The fminsearch function is part of core MATLAB. Everyone has it.
EDIT — (4 Nov 2020 at 18:25)
The ‘t’ vector doesn’t look correct (or is incomplete).
When I used these data:
I = [0 -118.0269 -111.9433 -18.7367 65.1586 75.6806 23.5090 -33.7683 -49.4437 -21.8789 15.8776 31.2675 17.8934 -6.1924 -19.1290 -13.5555 1.3143 11.2887 9.7332 0.8686 -6.3867 -6.7010 -1.6242 3.4237 4.4505 1.6847 -1.6991 -2.8596 -1.4535 0.7399 1.7785];
t = linspace(0, 1E-6, numel(I));
and this ga (genetic algorithm, Global Optimization Toolbox) call:
% % % b(1) = V, b(2) = L, b(3) = C, b(4) = R
w = @(v) sqrt((1/(v(1).*v(2)))-(v(3)./(2.*v(1)))^2);
objfcn = @(b,t) (b(1)./(w([b(2),b(3),b(4)]).*b(2))).*exp(-(b(4)./(2.*b(2))).*t).*sin(w([b(2),b(3),b(4)]).*t);
ftns = @(b) norm(I - objfcn(b,t));
PopSz = 500;
Parms = 4;
opts = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms).*[1E-1 1E-10 1E-12 1E-2], 'MaxGenerations',2E4, 'PlotFcn',@gaplotbestf, 'PlotInterval',1);
[B,fval,exitflag,output] = ga(ftns, Parms, [],[],[],[],zeros(Parms,1),Inf(Parms,1),[],[],opts)
B = B.'
figure
plot(t, I, '-+b')
hold on
plot(t, objfcn(B,t), '-r')
hold off
grid
I got these parameters (the parameter mapping is the comment line just before the ga call.):
B =
4.353400463867188e+03
1.673999999729503e-07
2.389999309571067e-10
1.539550781250000e+00
and this plot:
That seems a reasonably good fit to me, and the difference in the ‘given’ parameters from the estimated parameters is likely the reason I could not get fminsearch to fit it using them. The fitness value (norm of the residuals) is 2.777214204499168e+01.