Problem with creating a code for a sin regression, please help!

4 ビュー (過去 30 日間)
Scott
Scott 2012 年 2 月 3 日
編集済み: Matt J 2013 年 9 月 29 日
Hey all,
I need to create a regression program, specifically a sin regression, for some research I'm working on. I've coded a function that correctly determines the sign squared error, but I'm having issues getting fminsearch to function correctly.
Here's what (I think) the issue is: my function that determines the sign squared error (I call sse) requires two input arguments, params and plotmatrix. Params is clearly the parameter values, and plotmatrix is what the fitted curve is compared to (the name is just a function of some of the other stuff in my project/code). However, when I try to call fminsearch in another function, there doesn't seem to be a way to pass plotmatrix into sse (which it need to do the sign squared error)... I can only give it params. As a result, no matter what I've tried I get an error saying that "input argument plotmatrix is undefined."
Can anyone help me out with this? I'm putting the code to both functions that I'm having issue with below. Thank you very much.
Here is the function that actually does the regression:
function [Parameters,r_squared] = sin_regression(params, plotmatrix)
Parameters=fminsearch(@sse, params, plotmatrix);
function Fitted_curve=Fitted_curve(x)
Fitted_curve=Parameters(1)+Parameters(2)*sin(Parameters(3)*x+Parameters(4));
end
%%Calculate R-squared value
avg=0;
for j=1:1:(rows)
avg=avg+plotmatrix(j,2);
end
avg=avg/rows
SS_tot=0;
for j=1:1:(rows)
SS_tot=SS_tot+(plotmatrix(j,2)-avg)^2;
end
SS_err=0;
for j=1:1:(rows)
SS_err=SS_err+(plotmatrix(j,2)-Fitted_curve(plotmatrix(j,1)))^2
end
r_squared=1-(SS_err/SS_tot);
end
And here is the function that calculates the sign squared error:
function sse=sse(params, plotmatrix)
A_initial=params(1);
B_initial=params(2);
C_initial=params(3);
D_initial=params(4);
function Fitted_curve=Fitted_curve(x)
Fitted_curve=A_initial+B_initial*sin(C_initial*x+D_initial);
end
I=find(plotmatrix(:,2));
[rowI,colI]=size(I);
[rows,cols]=size(plotmatrix);
Error=zeros(rows,2);
for j=1:1:(rowI)
Error(j,1)=j;
Error(j,2)=plotmatrix(I(j),2)-Fitted_curve(plotmatrix(I(j),1));
end
sse=Error(:)'*Error(:);
end

採用された回答

Walter Roberson
Walter Roberson 2012 年 2 月 4 日
I am confused about "sin" vs "sine" vs "sign"
Anyhow, change your line
Parameters=fminsearch(@sse, params, plotmatrix);
to
Parameters=fminsearch(@(parm) sse(parm, plotmatrix), params);
  2 件のコメント
Scott
Scott 2012 年 2 月 4 日
Yeah I probably screwed that up via a typo at some point, haha. I'll check and see if this works.
Scott
Scott 2012 年 2 月 4 日
Thanks that worked.

サインインしてコメントする。

その他の回答 (1 件)

Scott
Scott 2012 年 2 月 4 日
Any ideas guys? Help would be greatly appreciated, as I'm sort of stalled until I figure this out...

カテゴリ

Help Center および File ExchangeSpline Postprocessing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by