Info

此问题已关闭。 请重新打开它进行编辑或回答。

Combining separate files

1 次查看(过去 30 天)
buxZED
buxZED 2011-3-2
关闭: MATLAB Answer Bot 2021-8-20
??? Subscripted assignment dimension mismatch.
Error in ==> fminsearch at 205
fv(:,1) = funfcn(x,varargin{:});
Error in ==> p5final at 95
sig4=fminsearch('P5F',1.5);% minimse the cost function using 'fminsearch' giving
a predicted value for sigma4
i get this error for the coding
sig4=fminsearch('P5F',1.5);% minimse the cost function using 'fminsearch' giving a predicted value for sigma4
c44=exp(-z.^2/(2*(sig4)^2));% Use global value for 'z' and predicted value for sigma4 to find a forth predicted value for average plume concentration ratio 'c'
e4=c-c44;% calculate prediction error between global c (average plume concentration ratio) and the forth predicted plume concentration
>> P5F is a another file codes are
function J=P5F(Sig)
global c2 z
J=0;
for k=1:201;
s1=c2*(k);
s2=exp(-z(k)^2/(2*(Sig)^2));
J=J+(s1-s2)^2;
end

回答(2 个)

Walter Roberson
Walter Roberson 2011-3-2
The argument to fminsearch must be a function handle, not a string. Use @P5F instead of 'P5F'
  2 个评论
buxZED
buxZED 2011-3-2
still gives the same error
?? Subscripted assignment dimension mismatch.
Error in ==> fminsearch at 205
fv(:,1) = funfcn(x,varargin{:});
Error in ==> Q5 at 99
sig4=fminsearch(@P5Function ,1.5);% minimse the cost function using
'fminsearch' giving a predicted value for sigma4
Matt Tearle
Matt Tearle 2011-3-2
Actually, you *can* use a string... but you *should* use a function handle.

Matt Tearle
Matt Tearle 2011-3-2
What are your globals c2 and z? Not having access to them, I hard-coded in a scalar for c2 and a 201-element vector for z, and it worked fine for me.
While we're here, don't use globals. Do this instead:
function J=P5F(Sig,c2,z)
[etc]
Then, in p5final
c2 = <something>;
z = <something>;
f = @(sig) P5F(sig,c2,z)
sig4=fminsearch(f,1.5)

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by