How can I write these functions to the press

4 views (last 30 days)
Bilal Ates
Bilal Ates on 26 Jun 2020
Answered: Ameer Hamza on 26 Jun 2020
  3 Comments
Bilal Ates
Bilal Ates on 26 Jun 2020
Sorry I typed it wrong. I want to write in matlab
Bilal Ates
Bilal Ates on 26 Jun 2020
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

Sign in to comment.

Answers (1)

Ameer Hamza
Ameer Hamza on 26 Jun 2020
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

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!