-1

I've searched everywhere, and I can't find anything.

First of all, let me just say I have never used Mat Lab so I have no idea what I am doing what so ever.

I have tried a few things, but none have worked. Apparently y(0)=2 tries to create a list of 0 units with the value of 2?

Anyway, can someone help me? I need to write a flexible Euler's Method equation in Mat Lab to solve a few equations like this.

1) y' = 5-3sqrt(y) ; y(0)=2

With h= .1 , .05 , .025, .01 && t =.5 ,1 , 1.5 , 2 , 2.5 ,3

I have no idea what I am doing.

Acorbe
  • 8,367
  • 5
  • 37
  • 66
Tropical_Peach
  • 1,143
  • 3
  • 16
  • 29
  • 1
    I guess that there exist at least 1e6 web pages talking about explicit Euler numerical scheme and ways to implement it. Please post at least some (meaningful) lines of your solution. – Acorbe Oct 25 '12 at 07:00
  • I've looked at a lot of those, mainly ones like `t = 1; %adjust value of t y(0) = 2;%input your initial condition dt = 0.1; %adjust your step size T = 0:dt:5; %set up your time domain, here I have [0,5]` But when I use them, it returns with `ATTEMPTED TO ACCESS (0);must be a positive integer or logical"` I have no idea why or how to fix that. – Tropical_Peach Oct 25 '12 at 07:05
  • 2
    For someone with as little knowledge of Matlab as you both claim to have and seem to have implementing Euler's method is far too ambitious for a first project. Spend at least 2 working days familiarising yourself with Matlab culminating with writing a simple function, perhaps to find the roots of a polynomial and to plot them. Then you'll have the basic (Matlab) vocabulary to start thinking about implementing Euler's method along the lines suggested. – High Performance Mark Oct 25 '12 at 07:29
  • Ordinarily, I would take that advice; however I was handed this project a few hours ago and have to get it done within the next ~7 hours. I might be able to fan-dangle my way around this thing, the first program I wrote in C was a square root function. (At the time, I had no idea how to compile the math library.) Thanks anyway. – Tropical_Peach Oct 25 '12 at 07:32
  • 1
    Type `help ode45`, saves you a lot of trouble. Or is this badly-planned homework? – Rody Oldenhuis Oct 25 '12 at 10:00

1 Answers1

5

Let us suppose to implement the method with constant time step, say dt>0. If we wanna integrate the equation up to a time T>0, we consider a time discretization

tt = 0:dt:T;

We'd better pre-allocate our solution vector for speed purpose, i.e.

yy=zeros(1,length(tt));

yy will contain the first order-in-time approximation of the solution we will produce (i.e., with little abuse of notation,

yy(1)==y_r(t=0)

and

yy(end)==y_r(t=T) + global error,

where the function y_r=y_r(t) is our real solution).

Supposedly, we have a first order ODE in normal form, i.e.

 dy_r / dt = f(y_r;t)

and an initial datum

 y_r(t=0)=y_0

(i.e. we have a Cauchy problem). Thus, we should firstly initialize our solution vector

  yy(1) = y_0;

then, we can find the solution for future times, i.e.

  N = length(tt);
  for t = 2 : N        // we should look at future times, thus we start from 2
                       // NOTE: this is first order explicit Euler scheme.
       yy(t) = yy(t-1) + dt*f(yy(t-1),t);
  end

We're done. We can now plot the solution.

  plot(tt,yy);

Now the point is: are you satisfied with first-order-in-time accuracy?

Think that if you use this scheme to solve e.g. Hamiltonian problems (say the simple harmonic oscillator), it will give artificial excitation to your system (properly, you can see a drift out of your correct Hamiltonian orbit). In few words, after little time your solution is completely artificial.

Indeed, when you solve real problems, you have to carefully consider your problem and your physics, and then choose a proper numerical scheme to solve your equation. Soon, probably you will be asked to implement more accurate schemes such as Runge Kutta (which you can better trust, but just a little bit, at least in their original form).

Acorbe
  • 8,367
  • 5
  • 37
  • 66