error window to a model function after nlinfit

4 views (last 30 days)
Hi I use nlinfit to fit an equation to a model, and get the value of the parameter with its associated error. Now I want to use this parameter with its error and plot another function, which should show the error region as well! For example: Equation 1: y=Acos(x)+Bsin(x) is fitted using nlinfit to give this. A=5 +- 0.1 B=3+-0.2 Now I want to use these values of A and B (with errors) to plot a function Y+-delY=(A+-delA)cosx +(B+-delB)sinx where del is the error .
Thanks

Accepted Answer

John BG
John BG on 14 Mar 2017
Hi Debi
clear all
dx=.001;x=[-2*pi:dx:2*pi];
a0=5;
da=.1;
alow=a0-da;
ahigh=a0+da;
a=[alow:0.1*da:ahigh];
b0=3;
db=.2;
blow=b0-db;
bhigh=b0+db;
b=[blow:0.1*db:bhigh];
y2low=0;
y2high=0;
for k=1:1:numel(x)
y2=a*cos(x(k))+b*sin(x(k));
y2low = [y2low min(y2)];
y2high = [y2high max(y2)];
end
y=a0*cos(x)+b0*sin(x);
ny=[1:1:numel(y)];
plot(ny,y,'r');grid on
hold all;
plot(ny,y2low([2:end]),'b');
plot(ny,y2high([2:end]),'b');
.
the error function you want delY is
delY=y2high-y2low
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer
please click on the thumbs-up vote link
thanks in advance
John BG
  2 Comments
John BG
John BG on 14 Mar 2017
Thanks Debi, any more questions? if so feel free to ask
regards
John BG
DebiPrasad
DebiPrasad on 14 Mar 2017
Thanks John, I had some questions related to your answer. I have emailed you . Please let me know

Sign in to comment.

More Answers (2)

Star Strider
Star Strider on 14 Mar 2017
You have used the nlparci function to get the parameter confidence intervals.
The correct way to do what you want, that is to express the errors in the fit with respect to your data, is to calculate the confidence intervals on the prediction with the nlpredci function. That will give you the correct statistical result. It is also much more straightforward to implement.
The initially accepted answer is incorrect.
  2 Comments
DebiPrasad
DebiPrasad on 14 Mar 2017
Hi Star Strider, This question is actually a follow up to a previous question which was answered by you: https://uk.mathworks.com/matlabcentral/answers/324424-nlinfit-to-an-equation . The equation from where I get the paramter is not a simple fitting function, but is solved for minima to get the values of the parameters best fitting to raw data. Now the fit is not great if I keep one of the parameter constant, but fits well when all the paramter are free. However the value obtained do not match to the experiment. But if I keep one value fixed, the not good fit give me a reasonable value of the other value. So I want to see if the raw data is in the error estimate of the model data found from the parameters. Thanks Debi
Star Strider
Star Strider on 14 Mar 2017
Hi Debi,
The nlinfit function does not allow you to constrain the parameters. The lsqcurvefit function does.
If you have the Optimization Toolbox, see if using the lsqcurvefit function with constraints on the parameter you want to limit will give you an acceptable fit to your data. I believe you can use nlparci and nlpredci with constrained parameter estimation outputs returned by lsqcurvefit, and I see nothing in the documentation for either function that indicates it would not be appropriate. (I know you can use them with lsqcurvefit with unconstrained parameters.)

Sign in to comment.


Jan
Jan on 14 Mar 2017

Community Treasure Hunt

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

Start Hunting!