# Solving system of ODEs using Euler's method and 4 th order Runge Kutta method.

1 visualizzazione (ultimi 30 giorni)
tirah azman il 20 Giu 2021
Risposto: Gargi Patil il 2 Set 2021
Hi, im going to ask how to solve this problem?
Using time step, ℎ = 0.2, solve the model using Euler method and 4 th order Runge Kutta method. Plot the solutions
##### 2 CommentiMostra NessunoNascondi Nessuno
tirah azman il 21 Giu 2021
f=@(t,y) [(20*(-y(1) + Heaviside(y(1)-a) -y(2))) ; (y(1) - 0.15*y(2))];
y=[0.25;0];
h=0.2;
t=0;
a=0.2;
for i=1:1000
if y(1)<a
Heaviside((y(1)-a)) =0;
else
Heaviside(y(1)-a) =1;
end
plot(t,y(1),'linewidth','b.',t,y(2),'linewidth','r.'); hold on
s=f(t,y);
y=y+h*s;
t=t+h;
end
hold off
title('ERP')
xlabel('Time')
ylabel('mV')
tirah azman il 21 Giu 2021
the code still warning at heaviside fx

Accedi per commentare.

### Risposte (1)

Gargi Patil il 2 Set 2021
Hi,
The given code throws the error "Array indices must be positive integers or logical values.". The code in the if-else is written for Heaviside as an array rather than a function making (y(1) - a) an index value. Indices for an array can only be integers while the given index value is a fractional value.
This can be resolved by creating a user-defined function as follows:
function y = Heaviside(x)
if x < 0
y = 0;
else
y = 1;
end
end
MATLAB also has a pre-defined function heaviside which can be directly used.
Furthermore, the plot command will also throw an error as the property LineWidth expects positive numerical values. The code can be rectified by removing the property LineWidth or specifying a numeric value for it as follows:
plot(t,y(1),'b.',t,y(2),'r.');
%or
plot(t,y(1), 'b.',t,y(2), 'r.', 'LineWidth', 5);
For further information about code for solving ODEs using Runge-Kutta 4th Order Metod and Euler's Method, you can refer to the following resources:
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Runge Kutta Methods in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by