Question

Consider the following function sin(x) Using the following parameters in your functions: -func: the function/equation that yo

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

function I = trapezoidalRule(func,a,b,n)

h = (b-a)/n; % step size
F0 = func(a); Fn = func(b); %first and last elements
x = a;
sigma = 0;
for k=1:n-1
x = x+h;
sigma = sigma + func(x); % sum of intermediate elements
end

I = (h/2)*(F0+ 2*sigma + Fn); %final integral approximation

end

---------------------------------------------------------

function I = simpsonsRule(func,a,b,n)

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

F = 0;
for i=0:n
if i==0 || i==n % first and last coefficients
c =1;
elseif mod(i,2)~=0 %odd coefficients
c = 4;
elseif mod(i,2)==0 %even coefficients
c = 2;
end
  
F = F + c*func(a+i*h); % computing the sum
end

I = (h/3)*F; % final integral approximation
end

---------------------------------------------------------------

%driver file

clear
clc

% Task 1
func = @(x) 3 + (x.*sin(x))./(x-1);
a = 2; b = 7; n = 9;

% using trapezoidal rule
Itrap = trapezoidalRule(func,a,b,n)

% using simpson's rule
Isimp = simpsonsRule(func,a,b,n)

% using quad function
Iquad = quad(func,a,b)

%percentage error
ItrapPercentError = (abs(Iquad-Itrap)/Iquad)*100
IsimpPercentError = (abs(Iquad-Isimp)/Iquad)*100

%--------------------------------------------------------------

% Task 2

func = @(x) 6 + 3.*cos(x);
a = 0; b = pi/2;

disp('1 - composite trapezoidal rule')
disp('2 - composite simpson''s 1/3 rule')
rule = input('Which rule to use? ');
n = input('Enter number of points n: ');

if n>=2
switch rule
case 1
Itrap = trapezoidalRule(func,a,b,n)
Iquad = quad(func,a,b)
%percentage error
ItrapPercentError = (abs(Iquad-Itrap)/Iquad)*100
case 2
Isimp = simpsonsRule(func,a,b,n)
Iquad = quad(func,a,b)
IsimpPercentError = (abs(Iquad-Isimp)/Iquad)*100
otherwise
disp('invalid input')
end

else
disp('invalid number of points')
end



Itrap 13.8139 Isimp 13.0962 Iquad 13.7474 ItrapPercentError - 0.4841 IsimpPercentError 4.7369

1-composite trapezoidal rule 2 composite simpsons 1/3 rule Which rule to use? 1 Enter number of points n: 20 Itrap 12.4232 |

1 composite trapezoidal rule 2 composite simpsons 1/3 rule Which rule to use? 2 Enter number of points n: 20 Isimp = 12.4248

COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Add a comment
Know the answer?
Add Answer to:
Use Matlab code Consider the following function sin(x) Using the following parameters in your functions: -func:...
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