How can use Simpson\'s rule to write a matlab code or java code for below questi
ID: 3689294 • 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)
Explanation / Answer
You are absolutely right for entire program except the A equation...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.