??? Subscripted assignment dimension mismatch.

function [er]= ErrorF(x,y,p)
x=[-0.6 -0.4 -0.1 0.5 1.5];
y=[18 8 3 2 0.9];
er=y-(1./((x.*p(1))+p(2)));
end
ERROR:
??? Subscripted assignment dimension mismatch.
Error in ==> fminsearch at 191
fv(:,1) = funfcn(x,varargin{:});
Error in ==> Untitled at 3
p=fminsearch(@(p)er,[0.2 0.5])
need help with this.!!!!!

Answers (2)

You call fminsearch on @(p)er but you are not passing p to er. You have not shown the code for er; you have instead shown the code for ErrorF. If you have created a variable named "er" before the fminsearch() call, by calling ErrorF yourself, then notice that variable would be a scalar rather than a function.
Question: why would you pass x and y to ErrorF if you are going to immediately reassign their values?
Suggested code:
x=[-0.6 -0.4 -0.1 0.5 1.5];
y=[18 8 3 2 0.9];
ErrorF = @(p) y-(1./((x.*p(1))+p(2)));
pval = fminsearch(ErrorF, [0.2 0.5]);
try this is code
x=[-0.6 -0.4 -0.1 0.5 1.5];
y=[18 8 3 2 0.9];
fun1 = @(p,x)1./(x.*p(1)+p(2));
pout = nlinfit(x,y,fun1,[.2 .5]);
x1 = linspace(-.6,1.5,100);
plot(x,y,'ko',x1,fun1(pout,x1),'r-')

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 25 Feb 2012

Community Treasure Hunt

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

Start Hunting!