how to solve " Undefined function 'lt' for input arguments of type 'tf'. Error in ftfs (line 5) while (t < tf) ?"

3 ビュー (過去 30 日間)
the function is :
function []= ftfs()
j = 1;
t = 0;
while (t < tf)
u(1,j+1) = u(1,j) - (lambda*a).*(u(2,j)-u(1,j));
u(N,j+1)= u(1,j+1);
u(2:(N-1),j+1) = u(2:(N-1),j) - (lambda*a).*(u(3:N,j)-u(2:(N-1),j));
t = t + dt;
j = j + 1;
end
plot(x,u(:,i),'m-..-')
end
I am calling this function in this program :
a = input('Insert the value of a: ');
CFL = input('Insert the value of CFL: ');
tf = input('Insert final time : '); %final time
dx = input('Insert the value of dx: ');
dt = (CFL * dx)/ abs(a);
lambda = (dt/dx)
N = input('Insert the value of N: ');
x = linspace(-1,1,N);
%choose test case:
tc= input('choose test case number : ');
if tc == 1
u(:,1)= -sin(pi.*x)
end
plot(x,u(:,1),'k')
hold on
ftfs();
hold on

採用された回答

OCDER
OCDER 2017 年 12 月 11 日
編集済み: OCDER 2017 年 12 月 11 日
The problem lies in understanding the scope of each variable used in Matlab. Here's a read on that: http://matlab.izmiran.ru/help/techdoc/matlab_prog/ch12_nd5.html
Each function is like a new workspace, where it does NOT see the variables made OUTSIDE the function, unless passed as an input, used by a nested function, or used as global variables.
You did not specify what "tf" is in ftfs. So this while loop has no idea what the conditions are. This is what the function is seeing:
while (t < tf) translates to:
while t is less than an undefined function called tf (or a transfer function with no inputs)
To fix this, you need ftfs to take inputs.
function [] = ftfs(a, CFL, tf, dt, lambda, N, x, u)
And then you need to pass the variables into the function when you call ftfs.
hold on
ftfs(a, CFL, tf, dt, lambda, N, x, u);
hold on
Note that you will get an error if tc is unequal to 1, since then u is undefined. Also, your ftfs function does not know what i is, so you'll get another error.
  6 件のコメント
guetty abboud
guetty abboud 2017 年 12 月 11 日
thanks it worked very well
OCDER
OCDER 2017 年 12 月 11 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by