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

4 ビュー (過去 30 日間)
David W.
David W. 2014 年 6 月 7 日
コメント済み: Star Strider 2014 年 6 月 8 日
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.
  2 件のコメント
David W.
David W. 2014 年 6 月 8 日
thank you very much,with you suggestion ,I realised that 'eval' is not needed!
Star Strider
Star Strider 2014 年 6 月 8 日
My pleasure!
Glad we got that sorted!

サインインしてコメントする。

その他の回答 (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
David W.
David W. 2014 年 6 月 7 日
the error is in the 9th. row

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by