Undefined function or variable ' t ',how can I difine ' t ' correctly?

8 次查看(过去 30 天)
Hi!
I have defined a function which is:
function [ xs Fxs ] = RevNewton( F, grad, Hess, x, x0 )
where 'F' is the function of 'x'(can be a scalar or a vector),'x0' is the initial value of 'x','grad' and 'Hess' are the gradient vector and Hesse matrix respectively.
In this funtion,I used a varieble 't', which I haven't defined first, to transform the function F(x) to a function F(t) by using:
subs(F,{x},{x0+t.*p}),
where 'p' is a numerical vector.when using this function,I got an error:
??? Undefined function or variable 't'.
Error in ==> RevNewton at 10
fhandle=eval(['@(t)',vectorize(subs(F,{x},{x0+t.*p}))]);

采纳的回答

Star Strider
Star Strider 2014-6-7
If I understand ‘9 th row’ correctly, why not just state it as:
fhandle = @(t) vectorize(subs(F,{x},{x0+t.*p}));
Also consider using matlabFunction.

更多回答(1 个)

MA
MA 2014-6-7
put syms t before giving function F to the program
  6 个评论
David W.
David W. 2014-6-7
function [ xs Fxs ] = RevNewton( F, grad, Hess, x, x0 )
Hess
f0 = subs(F,{x},{x0});
g0 = subs(grad,{x},{x0});
HS=0;
while HS==0
G = subs(Hess,{x},{x0});
if det(G)==0
p=-g0;
fhandle=eval(['@(t)',vectorize(subs(F,{x},{x0+t.*p}))]);
[a b]=searchint(fhandle,0,1);
[tg f]=goldenSM(fhandle,0.1,a,b);
xx=x0+tg.*p;
g=subs(grad,{x},{xx});
else
x1=x0-G\g0;
f1=subs(F,{x},{x1});
[xx f g]=revise(F,f0,f1,g0,x0);
end
HS = HS(x0,xx,f0,f,g);
x0 = xx;
f0 = f;
g0 = g;
end
xs = xx;
Fxs = f;
function [xx f g]=revise(F,f0,f1,g0,x0)
if f1<f0
g=subs(grad,{x},{x1});
f=f1;
xx=x1;
else
if abs(g0.*p)/(norm(g0,2)*norm(p,2))<=0.1
p=-g0;
else
if (g0.*p)/(norm(g0,2)*norm(p,2))<=0.1
p=-G\g0;
else
p=G\g0;
end
end
fhandle=eval(['@(t)',vectorize(subs(F,{x},{x0+t.*p}))]);
[a b]=searchint(fhandle,0,1);
[tg f]=goldenSM(fhandle,0.1,a,b);
xx=x0+tg.*p;
g=subs(grad,{x},{xx});
end
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by