Why do I keep getting this error message?
显示 更早的评论
I need to approximate the Jacobian. Here is my code, I cant seem to understand function handles very well, please help me!
function [ Jac ] = finitediffJac( usrfun, method, x, h ) [xrow, xcol]=size(x); if xcol>1 error('too many coloumns'); end xp=x; %[funrow,funcol]= size(feval(userfun,x)); if funcol>1 error('user function has too many columns.'); end Jac=zeros(funrow,xrow); %user inputs desired function, method of choice, a value for x, and a step % [hrow,hcol]= size(h); if hrow~=xrow error('h needs to have the same rows as x'); end for xcount=1:xrow if strcmpi(method, 'forward');
%if the user types in forward method the following finite differncing
%method will be used
Jac(:,xcount)=feval(userfun,(xp +h(xcount)))-feval(usrfun,xp)./h(xcount);
elseif strcmpi(method, 'backward');
%if the user types in backward method the following finite differncing
%method will be used
Jac(:,xcount)= (feval(usrfun,xp)- feval(usrfun,(xp-h(xcount))))./h(xcount);
elseif strcmpi(method, 'central');
%if the user types in central method the following finite differncing
%method will be used
Jac(:,xcount)= (feval(usrfun,(xp +h(xcount)))-feval(usrfun,(xp-h(xcount))))./2*h(xcount);
else
disp(' method not recognized')
end
end
end
*Terminal input and output***
>> usrfun = {@(x) (7*x(1)-x(1)*exp(x(3))+37);@(x)(sqrt(x(2)*x(3)));@(x) (12*x(1)*x(3).^3-6*x(2) +37);}
usrfun =
@(x)(7*x(1)-x(1)*exp(x(3))+37)
@(x)(sqrt(x(2)*x(3)))
@(x)(12*x(1)*x(3).^3-6*x(2)+37)
>> function [ Jac ] = finitediffJac( usrfun, 'forward', x, h )
function [ Jac ] = finitediffJac( usrfun, 'forward', x, h )
|
??????Error: Function definitions are not permitted in this context.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!