The Student Room Group

Numerical Methods coding -- where am I wrong??

I gotta essentially create code on MATLAB for the Trapezium rule, and Simpson's 1/3 rule. I have done this. Then I gotta test these on a certain function ff between two bounds and plot the errors for using both methods.

Now, I'm pretty sure I'm going wrong somewhere since my trapezium errors are smaller than simpson's errors! Simspson's is supposed to be more accurate so I think I'm going wrong somewhere but I dunno where??

Can someone check my code for me? Thx!

https://imgur.com/a/r6Y5BPd
Original post by LeTroll
I gotta essentially create code on MATLAB for the Trapezium rule, and Simpson's 1/3 rule. I have done this. Then I gotta test these on a certain function ff between two bounds and plot the errors for using both methods.

Now, I'm pretty sure I'm going wrong somewhere since my trapezium errors are smaller than simpson's errors! Simspson's is supposed to be more accurate so I think I'm going wrong somewhere but I dunno where??

Can someone check my code for me? Thx!

https://imgur.com/a/r6Y5BPd
Don't know Matlab, but looking at the Simpson's rule code, it looks as if sum2 contains terms f(2)+f(4)+...+f(n); shouldn't this be only f(2)+f(4)+...+f(n-2)?
Reply 2
Original post by DFranklin
Don't know Matlab, but looking at the Simpson's rule code, it looks as if sum2 contains terms f(2)+f(4)+...+f(n); shouldn't this be only f(2)+f(4)+...+f(n-2)?


Aw crap you're right!! Thanks!

It looks much better now
Original post by LeTroll
Aw crap you're right!! Thanks!

It looks much better now
Cool. As a bit of advice from a programmer: in loops, always pay particular attention to what happens at the end: are you missing a point, or having one point too many? It's a *very* common place for errors to occur.

Quick Reply

Latest