Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can use Simpson\'s rule to write a matlab code or java code for below questi

ID: 3006793 • Letter: H

Question

How can use Simpson's rule to write a matlab code or java code for below question?

Question:

Find the area when n = 2. I wrote below code by matlab, but I think it is not completely correct.Can you find problem for me ?

f=@(x)(x*sin(x)^2)

format long

a=0

b=3

n=2

dx=(b-a)/n

sum_even = 0;

for i = 1:n/2-1

x(i) = a + 2*i*dx;

sum_even = sum_even + f(x(i));

end

sum_odd = 0;

for i = 1:n/2

x(i) = a + (2*i-1)*dx;

sum_odd = sum_odd + f(x(i));

end

A = dx*(f(a)+ 2*sum_even + 4*sum_odd +f(b))/3

******************************

A=3.01486102991289 (I believe this is wrong)

//A tutor said my code is correct expect the last A equation but he did not said any specific error.//

Explanation / Answer

Your code is correct. Yes for n=2 it does not give correct value but as you increase n it gets closer to the correct value of the integral which is: ~2.46454033

This is the value your code gives for n=20

2.46454918580 which is correct to 5 decimal places.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote