Problem with creating a code for a sin regression, please help!
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Scott
am 3 Feb. 2012
Bearbeitet: Matt J
am 29 Sep. 2013
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 4 Feb. 2012
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);
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Spline Postprocessing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!