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

4 visualizzazioni (ultimi 30 giorni)
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}))]);

Risposta accettata

Star Strider
Star Strider il 7 Giu 2014
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 Commenti
David W.
David W. il 8 Giu 2014
thank you very much,with you suggestion ,I realised that 'eval' is not needed!

Accedi per commentare.

Più risposte (1)

MA
MA il 7 Giu 2014
put syms t before giving function F to the program
  6 Commenti
David W.
David W. il 7 Giu 2014
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

Accedi per commentare.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by