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

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}))]);

 采纳的回答

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.

2 个评论

thank you very much,with you suggestion ,I realised that 'eval' is not needed!

请先登录,再进行评论。

更多回答(1 个)

put syms t before giving function F to the program

6 个评论

I have tied this,but also get an error:
??? Error using ==> assignin
Attempt to add "t" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in ==> syms at 77
assignin('caller',x,sym(x));
Error in ==> RevNewton at 6
syms t;
you should get answer with syms x t
If you don't mind,I can give you the full code and you can run it ,it's very simple,only that one error.
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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by