0

I tried to write the 'dumb' version of Euler's method using Matlab but I always came up with nothing. My code was trash :-(

See this pseudocode for this method :

‘set integration range
xi = 0
xf = 0
‘initialize variables
x = xi
y = 1
‘set step size and determine
‘number of calculation steps
dx = 0.5
nc = (xf – xi) / dx
‘ output initial condition
PRINT x, y
‘Loop to implement Euler’s method
‘and display results
DOFOR I = 1, nc
dydx = -(2X**3) + (12X**2) - (20X) + 8.5
y = y + dydx . dx
x = x + dx
PRINT x, y
END DO

I'm pretty sure that this pseudocode is what I need to implement, but I failed to convert it into Matlab code. Any help please ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
iTurki
  • 16,292
  • 20
  • 87
  • 132
  • 4
    `DOFOR I = 1, nc`, yet `nc = (xf – xi) / dx`, where `xi = 0` and `xf = 0`... 'nuff said – Rasman Jun 03 '11 at 14:01
  • 5
    Why don't you show your Matlab code? If this is homework, tag it as such. Thanks – eat Jun 03 '11 at 14:04
  • 3
    So what failed? Did you fail to even bother to try? Show some code, as otherwise you will probably fail to find someone to do all of your work for you. –  Jun 03 '11 at 15:39
  • Thanks Rasman. The problem was with the pseudocode not with the Matlab code. I thought I had a problem with the for-loop. Now it is clear – iTurki Jun 05 '11 at 06:36

1 Answers1

1

I am not sure where you're failing, it would really help to know how you're failing or what's a complication. Otherwise this just seems like doing your homework.

Here's my attempt at the MATLAB code. (Note: I do not have MATLAB on this computer and have not tested it)

i = 0;
stepsize = .1; % Define as what you want it to be
y = 1; % Initial value condition given 
t = 0; % Initial time value
yout = [zeros(1,20)]; % Assuming you want 20 outputs, can change
fvec = [zeros(1,20)];

for i = 1:20 % Time range, can change to correspond to what you want
fvec(i) = 2 - exp(-4*t) - 2*yout(i); % Just loops calculating based on Euler's method
yout(i+1) = yout(i) + stepsize*fvec(i)
t = t+stepsize % Time has passed, increment the time.
end

I'm not entirely sure this code, but it should give you an example of how to go about this. Please comment if something is wrong.

Eugene K
  • 3,381
  • 2
  • 23
  • 36
  • Thanks for the try. I guess my problem was with the pseudocode not with the Matlab code. Now it works fine. I guess your code will do the job also. Thanks – iTurki Jun 05 '11 at 06:37