MATLAB Answers

## How to use lsqcurvefit to find constant values?

Asked by R7 DR

### R7 DR (view profile)

on 16 Mar 2015
Latest activity Commented on by Star Strider

### Star Strider (view profile)

on 16 Mar 2015
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

#### 0 Comments

Sign in to comment.

## 1 Answer

### Star Strider (view profile)

Answer by Star Strider

### Star Strider (view profile)

on 16 Mar 2015
Edited by Star Strider

### Star Strider (view profile)

on 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’.

Star Strider

### Star Strider (view profile)

on 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.
R7 DR

### R7 DR (view profile)

on 16 Mar 2015
thanks for your help.
I will check my model.
Star Strider

### Star Strider (view profile)

on 16 Mar 2015
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?

Sign in to comment.