calling a function from a script

2 ビュー (過去 30 日間)
Ilias Minas
Ilias Minas 2021 年 12 月 18 日
コメント済み: Torsten 2021 年 12 月 19 日
Hi all,
I have a differential equation which I want to solve using Euler method.
I created a script which has only the equation:
function dx=differential (t,x);
dx/dt=-x+1/(1+exp(-5*(x-1)));
end
And another script in order to implement Euler's method
f = input('differential.m'); % Enter the function right to the ODE
t0 = input ('Enter initial value of time'); % Value that time starts
x0 = input ('Enter initial value of x'); % Initial condition
tn = input ('End point of time'); % Value that time ends
h = input ('Enter step size'); % TIme step value
% Euler method & Initial conditions
t(1)= t0;
x(1)= x0;
% Euler Loop
for i=1:n
x(i+1) = x(i) + h*f(t(i),x(i));
t(i+1) = t0 + i*h;
end
I want when I press play to the script of Euler's solution, instead of putting the differential equation manually, to call it direclty from the script which includes the equation.
How can I do this, and in which form should the equation be written in order to achieve this?
Thank you very much
Yours sincerely
Ilias Minas

採用された回答

Geoff Hayes
Geoff Hayes 2021 年 12 月 18 日
@Ilias Minas - can't you just replace f with differential like
t0 = input ('Enter initial value of time'); % Value that time starts
x0 = input ('Enter initial value of x'); % Initial condition
tn = input ('End point of time'); % Value that time ends
h = input ('Enter step size'); % TIme step value
% Euler method & Initial conditions
t(1)= t0;
x(1)= x0;
% Euler Loop
for i=1:n
x(i+1) = x(i) + h*differential(t(i),x(i));
t(i+1) = t0 + i*h;
end
  4 件のコメント
Voss
Voss 2021 年 12 月 19 日
The function 'differential':
function dx=differential (t,x);
dx/dt=-x+1/(1+exp(-5*(x-1)));
end
contains a syntax error: 'dx/dt' is not a valid expression for assignment
Torsten
Torsten 2021 年 12 月 19 日
function dx = differential (t,x);
dx = -x+1/(1+exp(-5*(x-1)));
end

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by