Euler's Method in MATLAB: Solving for dy/dt = cos(y)

Click For Summary
SUMMARY

This discussion focuses on implementing Euler's Method in MATLAB to solve the differential equation dy/dt = cos(y). The provided MATLAB code initializes parameters such as the initial condition y0 = 0, time step dt = 0.01, and number of points npoints = 500. The loop iteratively calculates the numerical solution, which is then plotted alongside the exact solution. The graph demonstrates that the solution asymptotically approaches π/2, indicating a limitation of Euler's Method in handling this behavior.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of numerical methods, specifically Euler's Method
  • Basic knowledge of differential equations
  • Experience with plotting in MATLAB
NEXT STEPS
  • Explore MATLAB's built-in functions for solving differential equations, such as ode45
  • Learn about stability analysis in numerical methods
  • Investigate alternative numerical methods like Runge-Kutta for improved accuracy
  • Study the behavior of solutions to nonlinear differential equations
USEFUL FOR

Students, educators, and researchers in mathematics and engineering who are interested in numerical methods for solving differential equations, particularly those using MATLAB.

JCienfuegos
Messages
7
Reaction score
0
Hello
I have a program for Eulers method >>

% Euler's Method for dy/dt = cost
k = 1;
y0 = 0;


npoints = 500;
dt = 0.01;

y = zeros(npoints,1); % this initializes the vector y to being all zeros
t = zeros(npoints,1);

y(1) = y0; % the initial condition
t(1) = 0.0;

for step=1:npoints-1 % loop over the timesteps
y(step+1) = y(step) + dt*k*(cos(y(step)));
t(step+1) = t(step) + dt;
end

plot(t,y,'r'); %plots the numerical solution in red
hold on; %keep the previously plotted lines
plot(t,yexact2(t)); %plots the exact solution (default plot is in blue, solid line)

The graph asymptotically approaches pi/2, and I can't think of a way to deal with this.
 
Physics news on Phys.org
It sounds like you're getting the correct graph
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K