Question

In Matlab please

Given the function: So sin? (x)dx calculate the integral for all possible combinations of Simpsons 1/3 and Simpsons 3/8 rul

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

clc

clear all

close all

format long

f=@(x) sin(x)^3;

I1=SimpsonComp(f,1,6,7);

I2=SimpThreeEight(f,1,6,7);

disp('Average is');

disp((I1+I2)/2)

function int=SimpsonComp(f,a,b,n)

%takes in parameter as function handel f, starting interval a and ending

%interval b for integration and number of intervals as n

h=(b-a)/n;%step of integration

fa=f(a);%value of function at a

fb=f(b);%value of function at b

ff=0;%initialize ff to 0

for i=2:2:n%traversing on even numbers i

x=(a+(i-1)*h);%accessing even points in the intervals divided

fx=feval(f,x);%value of function at x

ff=ff+4*fx;%add 4*fx to ff

end

for i=3:2:n%traversing on odd numbers i

x=(a+(i-1)*h);%accessing odd points in the intervals divided

fx=feval(f,x);%value of function at x

ff=ff+2*fx;%add 4*fx to ff

end

int=(h/3)*(fa+fb+ff);%finally add first and last points tto all the sum and divide by 3

end

function I = SimpThreeEight(f, a, b, n)

h = (b-a)/n;

S =feval(f,a);

for i = 1:3:n-2

x(i) = a + h*i;

S = S + 3*feval(f, x(i));

end

for i = 2:3:n-1

x(i) = a + h*i;

S = S + 3*feval(f, x(i));

end

for i = 3 : 3: n-3

x(i) = a + h*i;

S = S + 2*feval(f, x(i));

end

S = S + feval(f, b);

I = 3*h*S/8;

end

_ ww 26 end 27 int=(h/3)*(fa+fb+ff);%finally add first and last points tto all the sum and divide by 3 28 end 29 function I =

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
In Matlab please Given the function: So sin? (x)dx calculate the integral for all possible combinations...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT