How to use lsqcurvefit to find constant values?

Hi
How to use 'lsqcurvefit' to find the coefficient values 'a' and 'b'.
The blue line(A1) is produced from the experimental data and the green line is based on the 'nlinfit' function. But the error is very high.
Is it possible to use 'lsqcurvefit' and find the constant values?
A1=[......] % experimental data
A2= a*exp(b/X)*Y %function for greenline.
X=[.........] Y=[.........] I know the values of X and Y.
The ultimate aim is to reduce the error and finding the best fitted constant values.
Thanks

Risposte (1)

Star Strider
Star Strider il 16 Mar 2015
Modificato: Star Strider il 16 Mar 2015
You have to create a single matrix of your ‘X’ and ‘Y’ values:
XY = [X(:) Y(:)];
Then create your objective function ‘A2’ as:
% b(1) = a, b(2) = b
A2 = @(b, XY) b(1) .* exp(b(2)./XY(:,1)) .* XY(:,2);
And give it to lsqcurvefit as:
B0 = randi(10, 2, 1); % Choose Appropriate Initial Parameter Estimates
B = lsqcurvefit(A2, B0, XY, A1);
Where ‘B(1)=a’ and ‘B(2)=b’.

8 Commenti

R7 DR
R7 DR il 16 Mar 2015
Modificato: R7 DR il 16 Mar 2015
Hi Thanks for the reply.
I tried to code as per your instructions but I am getting the different constant values.
I am attaching the excel file, can you please check once.
With lsqcurvefit' I am getting the good values but still lot of error between the experimental and calculated data. Is there any other method to deal with it?
When I read it, I get a (1117x13) double array as output. I can’t run my code to test it until I know what rows or columns correspond to ‘X’, ‘Y’ and ‘A1’.
R7 DR
R7 DR il 16 Mar 2015
Modificato: R7 DR il 16 Mar 2015
Hi
In the Excel,
in Sheet1 A1=columnA; X=columnB; Y=columnC;
This code works, but doesn’t give what I consider a good fit:
data = xlsread('R7 DR Test.xlsx');
A1 = data(:,1);
X = data(:,2);
Y = data(:,3);
XY = [X Y];
b = rand(2,1);
A2 = @(b, XY) b(1) .* exp(b(2)./XY(:,1)) .* XY(:,2);
B0 = randi(1000, 2, 1)-500; % Choose Appropriate Initial Parameter Estimates
B = lsqcurvefit(A2, B0, XY, A1);
figure(1)
plot3(X, Y, A1, 'bp')
hold on
plot3(X, Y, A2(B,XY), '-r')
hold off
grid
It is most likely that your model ‘A2’ does not accurately describe the process that created your data.
I would consider a different model.
R7 DR
R7 DR il 16 Mar 2015
Modificato: R7 DR il 16 Mar 2015
Thanks for the code.
With 'nlinfit' function. I am getting satisfactory values but the error is high as shown in the above image. Can we try 'lsqnonlin' function?
Star Strider
Star Strider il 16 Mar 2015
Modificato: Star Strider il 16 Mar 2015
Using lsqnonlin would not get you anywhere, since lsqcurvefit is a wrapper for lsqnonlin that makes it easier to do curve fitting. They’re actually the same function.
Changing the curve fitting algorithm is not the appropriate approach. You need to change your model to one that most accurately describes the process that created your data. Your current model does not seem to do that.
What are you plotting in the figure you posted? I don’t get anything close to what you plotted with my code.
thanks for your help.
I will check my model.
My pleasure.
I get a significantly different plot from the figure you posted. What data did you use to create it?
What figure do you get when you use my code?

Accedi per commentare.

Richiesto:

il 16 Mar 2015

Commentato:

il 16 Mar 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by