Exploring Euler Method: Solving y'=x^2-y^2 w/ MATLAB

In summary, the author is looking at a code example for solving a first order differential equation. They are having difficulty with coding the "n" part of the equation.
  • #1
Void123
141
0
I'm looking at the following example (http://www.cyclismo.org/tutorial/matlab/control.html):

y'=x^2-y^2, y(0)=1, where they use the Euler method to approximate numerical solutions.

This is the code:

>> h = 0.1;
>> x = [0:h:2];
>> y = 0*x;
>> y(1) = 1;
>> size(x)

ans =

1 21

>> for i=2:21,
y(i) = y(i-1) + h*(x(i-1)^2 - y(i-1)^2);
end
>> plot(x,y)
>> plot(x,y,'go')
>> plot(x,y,'go',x,y)

I don't understand the third and fourth line at the top. What is [tex]y = 0*x[/tex] and how did they get y(1) = 1...?

Thanks.
 
Last edited:
Physics news on Phys.org
  • #2
You want to make sure that the size of the 'y' vector is the same as the size of the domain vector x so you can plot one against the other one.. So they start with y=0*x which should make y a vector of all zeroes that is the same length as x. Setting y(1)=1 means that the first entry of y will be 1. This is the same thing as saying that y, at the value of x that is the first one recorded in the domain vector, should be 1 (so all they've done is chosen an initial condition)
 
  • #3
Ok. I want to put [tex]y''(x) + \left(\frac{2}{x}\right) y'(x) + y^n = 0[/tex]

into the following template:

Code:
function[y,t] = euler(fun,y0,t0,T,h)
% [y,t] = euler(fun,y0,t0,T,h) -
%
% This function computes the solution to the IVP y'(t) = fun(y,t),
% y(t0)= y0 , for a given function "fun(t,y)" using Euler's method.
%
% The function is defined either via an m-file fun.m, or using the
% "inline" command. y0 is the initial value, T is the maximum time, h
% is the stepsize and t0=initial time.
%
% The output is a vector containing the approximate solution y_euler.
%
y(1) = y0;
t(1) = t0;
for i=1:T/h
y(i+1) = y(i) + h*feval(fun,t(i),y(i));
t(i+1) = t(i) + h;
end;
t=t’;
y=y’;
% End of m-file euler.m
end

There are a few difficulties. First, I am working with a second order differential equation. Secondly, I am interested in a continuous range of solutions for [tex]n[/tex]. For certain values of n, I have a transformation method that simplifies my equation by eliminating the y'(x). But, the main problem I'm dealing with is coding the "n" part. Whenever I try, an error message in the command box comes up that says 'matrix must be squared' and I don't know what this means.

I would appreciate any help.

Thanks.
 

1. What is Euler's Method and how is it used to solve differential equations?

Euler's Method is a numerical method for approximating solutions to differential equations. It involves breaking down a continuous function into smaller segments and using the slope of the function at each point to estimate the next point. This process is repeated until the desired accuracy is achieved.

2. How is Euler's Method implemented in MATLAB?

In MATLAB, Euler's Method can be implemented using a for loop and the formula: y_n+1 = y_n + h*f(x_n, y_n), where y_n is the previous value of the function, h is the step size, and f(x_n, y_n) is the differential equation being solved.

3. What are the advantages and disadvantages of using Euler's Method to solve differential equations?

The main advantage of using Euler's Method is that it is a relatively simple and easy to understand method. It also requires minimal computing resources. However, it can be less accurate than other numerical methods and may not work well for highly non-linear functions.

4. How do I know if my MATLAB code for Euler's Method is accurate?

One way to check the accuracy of your code is to compare the results to known analytical solutions for the same differential equation. You can also try using smaller step sizes and comparing the results to see if they are converging to a certain value.

5. Can Euler's Method be used to solve any type of differential equation?

No, Euler's Method is best suited for solving first-order, initial value problems. It may not be as effective for higher order or boundary value problems, as these require more sophisticated numerical methods.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
995
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top