How can I write these functions to the press

1 次查看(过去 30 天)
  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 CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by