Problem with creating a code for a sin regression, please help!
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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);
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!