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

In summary, the conversation discusses using Euler's Method to solve a differential equation and the issue of the graph approaching pi/2.
  • #1
JCienfuegos
8
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
  • #2
It sounds like you're getting the correct graph
 

1. What is Euler's Method?

Euler's Method is a numerical method for solving ordinary differential equations (ODEs). It uses the derivative of a function to approximate the value of the function at different points.

2. How does Euler's Method work?

Euler's Method works by taking small steps along the curve of a function, using the derivative at each step to calculate the value of the function at the next step. This process is repeated until the desired accuracy is achieved.

3. What is the equation used in Euler's Method?

The equation used in Euler's Method is yn+1 = yn + hf(xn, yn), where yn is the value of the function at the current step, h is the step size, and f(xn, yn) is the derivative of the function at the current step.

4. What is the purpose of using MATLAB to solve for dy/dt = cos(y) using Euler's Method?

MATLAB is a powerful computational tool that can perform complex calculations quickly and accurately. Using MATLAB to solve for dy/dt = cos(y) using Euler's Method allows for efficient and accurate solutions to be obtained, making it a useful tool for scientists and engineers.

5. What are the advantages of using Euler's Method in MATLAB?

Some advantages of using Euler's Method in MATLAB include its ease of implementation, its ability to handle complex ODEs, and its ability to quickly generate accurate solutions for a wide range of initial conditions. Additionally, MATLAB allows for the visualization of the solutions, making it easier to interpret and analyze the results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
939
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Introductory Physics Homework Help
Replies
5
Views
3K
Back
Top