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 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