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 CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by