I am using euler's method to solve a differential equation, but when I run the code it doesn't plot.
Show older comments
Hi, I am using Euler's method to solve the differential equation dv/dt, but when I run the code it doesn't plot andI don't get an error.
Any help would be greatly appreciated. Thanks
%Eulers method
t0=0; %start time
t1= 500; %finish time
dt = 100000; %large timestep to avoid errors
n = (t1-t0)/dt +1; %Number of nodes
t = linspace(t0,t1,n);%Time array
n_rou = round(n); %rounds n to an integer for y_euler to work
v_euler = zeros(n_rou,1); %create an empty array
v_euler(1) = 1; %initial value as v(0)=1
dvdt = @(v,t) -2.4*v(t)^2 - v(t)^3-4*v(t)+7.4 ; %anonymous function for dv/dt
for i = 1:n-1
f = dvdt(v_euler(i),t(i)); %Evaluate f=dy/dt at i
v_euler(i+1) = v_euler(i) + dt*f; % Evaluate y at i+1
end
plot(t,v_euler)
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!