??? 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.!!!!!
回答(2 个)
Walter Roberson
2012-2-25
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]);
Andrei Bobrov
2012-2-25
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-')
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!