Beginner: how to use nlinfit to set parameters and curve fitting?

Dear,
I am fresh with matlab and only followed tutorial with 2 weeks on examples.
I have a assignment for setting parameters and curve fitting.
However, I stucked in the first step, anybody could please help me out?
Thank you in advance.
t=[0 60 120 180 240 300 360 420 480 540 600 660 720]';
q=[295 161 99.1 68.2 50.2 38.6 30.6 24.8 20.5 17.2 14.7 12.6 11]';
plot(t,q,'ko');
xlabel('time(days),t');ylabel('production rate(bbl/day,q');
modelFun=@(b,t)(250/(1+b(1)*t/b(2))^(1/b(1));
beta0=[2,1];
beta=nlinfit(t,q,modelFun,beta0);
yhat = modelFun(beta,t);
figure(1)
plot(t,q,'xb')
Joe

6 Comments

Perhaps going through the examples in the documentation and thinking through each step will be helpful.
Any time you get an error message you should include it in your question. When I ran your code I get this error message. As the message indicates, "check for mismatched delimiters". What's that mean? a newbie may ask. Google's first link led me to the explanation. Long story short, you're missing a closed parenthesis.
modelFun=@(b,t)(250/(1+b(1)*t/b(2))^(1/b(1));
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check
for mismatched delimiters
Hi, Adam. thank you very much for your suggestion.
I have put read through the documentation all last night until 4 am...still had no clue and that is why I came here for help.
Also, I used parentheses and even put (250./(1+b(1).*t./b(2)).^(1./b(1))), there is still error information as metioned above.
I googled as well....can not find the way out.
I will do my best tonight again for the code. Thank you again for your help and suggestion.
When I run your code with the function you defined in the comment above, I do not get an error. My previous comment said,
"Any time you get an error message you should include it in your question."
Your previous comment claims to be getting an error message but I still don't see it 😕
Reiterating what Adam said, when I run the following code, which has your own fixed syntax in it, I get no error:
t=[0 60 120 180 240 300 360 420 480 540 600 660 720]';
q=[295 161 99.1 68.2 50.2 38.6 30.6 24.8 20.5 17.2 14.7 12.6 11]';
plot(t,q,'ko');
xlabel('time(days),t');ylabel('production rate(bbl/day,q');
modelFun=@(b,t)(250./(1+b(1).*t./b(2)).^(1./b(1)));
beta0=[2,1];
beta=nlinfit(t,q,modelFun,beta0);
yhat = modelFun(beta,t);
figure
plot(t,q,'xb')
Be sure that you saved the code change before you ran it.
Hi Adam and cyclist, thank you both!
I followed Adam's suggestion and ....yes, I did not really save it ( actually, I used to ctrl+s all the time, however, I did not put the right one in the right folder).
I recopied the whole code and built a new .m, then. No error.....
I learned a lot not only for the code....but also....carefulness.
Thank you both again. I am so excited for solving the a and b!! by following your instruction of both!! Thank you.
HOWEVER, I spent whole last night for second part, really have no idea how to do curve-fitting.
I will re-read through the link provided by Adam and hopefully I could fit the curve appropriately.
Sounds like you're on the right path! Keep going!

Sign in to comment.

Answers (0)

Asked:

on 24 Sep 2020

Commented:

on 25 Sep 2020

Community Treasure Hunt

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

Start Hunting!