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

1 次查看(过去 30 天)
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'

采纳的回答

Sean de Wolski
Sean de Wolski 2012-8-9
编辑:Sean de Wolski 2012-8-9
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 个评论
Teja Muppirala
Teja Muppirala 2012-8-13
编辑:Teja Muppirala 2012-8-13
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 2012-8-13
编辑:Peyman 2012-8-13
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, ....

请先登录,再进行评论。

更多回答(1 个)

Tom Lane
Tom Lane 2012-8-9
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 个评论
Tom Lane
Tom Lane 2012-8-10
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 2012-8-13
编辑:Peyman 2012-8-13
(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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by