How to use CurveFitting coefficients in the rest of the code?

5 views (last 30 days)
I have this code:
(t,p)=coordination of out input data
f=fit(t,p,'sin2');
we know it will create this function :
f(t)=a1*sin(b1*t+c1)+a2*sin(b2*t+c2);
and uses it to do the fitting on our input data (t and p);
Now, I have some other set of data(t2), and want to use the same function f(t) and find the answer of f(t2);
How can I call coefficients later in the rest of the code with their value their are assigned to? (after the fitting is done they will have some values)
I have tried coeff = coeffnames(f) but it only prints name of the coefficients.
** f(t2) doesn't work; MATLAB shows error:
Undefined function or variable 'a1'

Accepted Answer

Sean de Wolski
Sean de Wolski on 9 Aug 2012
Edited: Sean de Wolski on 9 Aug 2012
You answered it yourself!
f(t2)
More
Whwere does a1 come from. Consider this:
t = 1:10;
p =2*(1:10)+randn(1,10)+5;
f = fit(t',p','poly1');
f(1:20)
  5 Comments
Teja Muppirala
Teja Muppirala on 13 Aug 2012
Edited: Teja Muppirala on 13 Aug 2012
Try plot(t1,f(t1)).
Anyways, copy and paste this into the command window.
clear
rng(0);
t = (1:10)';
p = 3*sin(2*t)+0.1*randn(10,1);
f=fit(t,p,'sin2')
t2 = (0:9)';
f(t2)
plot(t,f(t),'b',t2,f(t2),'r:')
f.a1, f.b1, f.c1
Does the above code not work perfectly (my MATLAB is Japanese, so ignore the Japanese characters...)?
f =
一般モデル Sin2:
f(x) = a1*sin(b1*x+c1) + a2*sin(b2*x+c2)
係数 (95% の信頼限界):
a1 = 3.021 (2.897, 3.145)
b1 = 2.03 (2.008, 2.053)
c1 = -0.1896 (-0.317, -0.06228)
a2 = 0.1413 (0.007181, 0.2753)
b2 = 1.165 (0.7756, 1.553)
c2 = -2.504 (-4.794, -0.2135)
ans =
-0.6535
2.7742
-2.0383
-1.0072
3.1299
-1.5717
-1.7773
2.9177
-0.9517
-1.9537
ans =
3.0212
ans =
2.0304
ans =
-0.1896
Peyman
Peyman on 13 Aug 2012
Edited: Peyman on 13 Aug 2012
Thanks Teja; Apparently the problem was just not mentioning "f(t2)" alone in a line.
Is there any way to let MATLAB to decide for best fitting curve?
like: sin, fourier, weibull, ....

Sign in to comment.

More Answers (1)

Tom Lane
Tom Lane on 9 Aug 2012
Although I don't understand what coeffvalues(f) doesn't work, nor why f(t2) doesn't work, nor where the a1 error message comes from, here's another solution nobody mentioned so far.
It is possible to use f.a1 to get at the value of a1, and of course a similar thing for the other parameters.
  3 Comments
Tom Lane
Tom Lane on 10 Aug 2012
You lost me. First, I suggest you try the code from Sean's example under the "more" heading and see what happens. His approach seems to be what you want. I don't see why you would get an error message mentioning a1 if you did that.
Second, I don't understand what is "just wrong." If you fit f to t and then look at f.a1, do you not get the same a1 value that you see in the display of f?
Peyman
Peyman on 13 Aug 2012
Edited: Peyman on 13 Aug 2012
(this is for "just wrong")
Here is how it should be (I have just copied and pasted coefficients from CurveFitting GUI and it works perfectly): https://docs.google.com/open?id=0Bzt6t5PRyt66T0hMNWY5bjhwRnc
But, this is how it becomes when I use f.a1, f.a2, ... for each coefficient;
Can you see the problem? I have used the fitting function created at the first part for the second part, it's the same function, but look how it operates!!! like linear! but the function is Sinusoidal of order 8.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!