MATLAB: Trying to figure out nonlinear regression code

MATLABnonlinear regression

Okay so this is my code, I do not see an error. The problem seems to be in the fminsearch function though.
function f=fSSR(a,xm,ym)
yp=a(1)*xm.^a(2);
f=sum((ym-yp).^2);clear
%15.22
%Nonlinear regression
x=[.5 1 2 3 4];
y=[10.4 5.8 3.3 2.4 2];
a=fminsearch(@fSSR,[1,1],[],x,y)
v=[0:100];
F=a(1)*v^a(2);
plot(F)

Best Answer

You got very close.
This works:
yp = @(a,xm) a(1)*xm.^a(2); % Objective Function
x=[.5 1 2 3 4];
y=[10.4 5.8 3.3 2.4 2];
f= @(a) sum((y-yp(a,x)).^2); % Sum-Squared-Error Cost Function
[a, SSE] = fminsearch(f,[1,1]); % Estimate Parameters (‘a’)
v = linspace(min(x), max(x)); % Smooth Approximation
figure(1)
plot(x, y, 'bp')
hold on
plot(v, yp(a,v), '-r')
hold off
grid
I used an anonymous function because it’s just easier. The ‘SSE’ output is the sum-squared-error at convergence.