How do I fit a function to data using LSQNONLIN or LSQCURVEFIT?
Show older comments
I would like to fit the following function to data I have using LSQNONLIN or LSQCURVEFIT.
My function is described in the code.
However, it always gives error.
a = 0.800;
b = 0.150;
c = 0.200;
x0 = 0.300;
x = 0.500;
y0 = 0.150;
y = 0.150;
z0 = 0.100;
z = 0.100;

tempodados = [ 0.0000755000
0.0000790000
0.0000825000
0.0000860000
0.0000895000
0.0000930000
0.0000965000
0.0001000000
0.0001035000
0.0001070000
0.0001105000
0.0001140000
0.0001175000
0.0001210000
0.0001245000];
energiadados = [-2.998561136
-2.047288671
-1.423256844
-0.555726195
0.877699417
2.200575773
3.082059591
3.558854247
3.711599564
3.666770625
3.676198031
3.880316305
4.303658902
4.80742401
5.157829146];
E = @(e,tempodados)[
log(e(3).*exp(-e(2).*tempodados).*(1+(2.*GA.*2.*GB.*2.*GC)+(2.*GA+2.*GB+2.*GC)+((2.*GA.*2.*GB)+(2.*GA.*2.*GC)+(2.*GB.*2.*GC))));
GA = 0;
for n = 1:1:500
ga = cos((n.*pi.*x)./a).*cos((n.*pi.*x0)./a).*exp(-e(1).*((n*pi/a).^2).*tempodados);
GA = GA + ga;
end
GB = 0;
for n = 1:1:500
gb = cos((n.*pi.*y)./b).*cos((n.*pi.*y0)./b).*exp(-e(1).*((n.*pi./b).^2).*tempodados);
GB = GB + gb;
end
GC = 0;
for n = 1:1:500
gc = cos((n.*pi.*z)./c).*cos((n.*pi.*z0)./c).*exp(-e(1).*((n.*pi./c).^2).*tempodados);
GC = GC + gc;
end
];
e0 = [1,1,1];
e = lsqnonlin(fun,e0,tempodados,energiadados)
1 Comment
Vaclav Rimal
on 26 Jun 2019
What error it actually produces?
Answers (1)
function main
tempodados = [ 0.0000755000
0.0000790000
0.0000825000
0.0000860000
0.0000895000
0.0000930000
0.0000965000
0.0001000000
0.0001035000
0.0001070000
0.0001105000
0.0001140000
0.0001175000
0.0001210000
0.0001245000];
energiadados = [-2.998561136
-2.047288671
-1.423256844
-0.555726195
0.877699417
2.200575773
3.082059591
3.558854247
3.711599564
3.666770625
3.676198031
3.880316305
4.303658902
4.80742401
5.157829146];
e0 = [1,1,1];
e = lsqcurvefit(@fun,e0,tempodados,energiadados)
end
function E = fun(e,tempodados)
a = 0.800;
b = 0.150;
c = 0.200;
x0 = 0.300;
x = 0.500;
y0 = 0.150;
y = 0.150;
z0 = 0.100;
z = 0.100;
GA = 0;
for n = 1:1:500
ga = cos((n.*pi.*x)./a).*cos((n.*pi.*x0)./a).*exp(-e(1).*((n*pi/a).^2).*tempodados);
GA = GA + ga;
end
GB = 0;
for n = 1:1:500
gb = cos((n.*pi.*y)./b).*cos((n.*pi.*y0)./b).*exp(-e(1).*((n.*pi./b).^2).*tempodados);
GB = GB + gb;
end
GC = 0;
for n = 1:1:500
gc = cos((n.*pi.*z)./c).*cos((n.*pi.*z0)./c).*exp(-e(1).*((n.*pi./c).^2).*tempodados);
GC = GC + gc;
end
E = log(e(3).*exp(-e(2).*tempodados).*(1+(2.*GA.*2.*GB.*2.*GC)+(2.*GA+2.*GB+2.*GC)+((2.*GA.*2.*GB)+(2.*GA.*2.*GC)+(2.*GB.*2.*GC))));
end
Categories
Find more on Nonlinear Least Squares (Curve Fitting) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!