How can I write these functions to the press

1 回表示 (過去 30 日間)
Bilal Ates
Bilal Ates 2020 年 6 月 26 日
回答済み: Ameer Hamza 2020 年 6 月 26 日
  3 件のコメント
Bilal Ates
Bilal Ates 2020 年 6 月 26 日
Sorry I typed it wrong. I want to write in matlab
Bilal Ates
Bilal Ates 2020 年 6 月 26 日
clc; % Clears the screen
clear all;
h=0.25; % step size
x = 0:h:1; % Calculates upto y(4)
y = zeros(1,length(x));
z = ???
y(0) = 2; % initial condition
z(0) = 4;
F_xy = ?????????? % change the function as you desire
F_xz = ??????????
for i=1:(length(x)-1) % calculation loop
k_1 = F_xy(x(i),y(i));
k_2 = F_xy(x(i)+0.5*h,y(i)+0.5*h*k_1);
k_3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2));
k_4 = F_xy((x(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h; % main equation
end

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

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 6 月 26 日
You can use ode45() to solve such a system of ODEs.
ic = [2; 4];
xspan = [0 1];
[x, Q] = ode45(@odeFun, xspan, ic);
y_sol = Q(:,1);
z_sol = Q(:,2);
plot(x, Q);
legend({'y', 'z'}, 'FontSize', 16);
function dQdx = odeFun(x, Q)
% Q(1) <=> y, Q(2) <=> z
y = Q(1);
z = Q(2);
dydx = -2*y + 4*exp(-x) + exp(-1000*z^2);
dzdx = -y*z^2/3;
dQdx = [dydx; dzdx];
end

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by