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
  • Can you please do this in matlab Consider the following function: 4 0 Using the following...

    Can you please do this in matlab Consider the following function: 4 0 Using the following parameters in your functions: func: the function/equation that you are required to integrate a, b: the integration limits n: the number of points to be used for the integration : Integral estimate A. Write a function capable of performing numerical integration of h(x) using the composite B. write a function capable of performing numerical integration of h(x) using the composite C. Calculate the absolute...

  • MATLAB Write an m-file capable of performing numerical integration for the equation using the simpsons and...

    MATLAB Write an m-file capable of performing numerical integration for the equation using the simpsons and trapezoidal functions: function [value, error] = simpsons(func, a, b, n, I) %retuns the value and error ret = 0; h = (b-a)/(n+1); %step size pts = a:h:b; % array of points for i=2:(n+3)/2 a = 2*i-3; b = 2*i-2; c = 2*i-1; ret = ret + (func(pts(a)) + 4*func(pts(b)) + func(pts(c))); end value = h*ret/3; error = abs(I - value)*100/I; %error between value and...

  • Show work by hand and also using MATLAB code. Model 1 Given a polynomial f(x) Write a first-order approximation of f(x), given the value of f(x) at two points Plot the polynomial and the first-or...

    Show work by hand and also using MATLAB code. Model 1 Given a polynomial f(x) Write a first-order approximation of f(x), given the value of f(x) at two points Plot the polynomial and the first-order approximation on a graph Write a second-order approximation of f(x), given the value at three points. Plot the polynomial, the first-order and second-order approximations on a graph Find the integral Exactly Using trapezoidal rule Using composite trapezoidal rule Using Simpson's 1/3 rule . Calculate the...

  • USE MATLAB ONLY NEED MATLAB CODE MATLAB 21.4 Integrate the following function analytically and using the...

    USE MATLAB ONLY NEED MATLAB CODE MATLAB 21.4 Integrate the following function analytically and using the trapezoidal rule, with - 1,2,3, and 4: 0 (x + 2/x dx 0 Use the analytical solution to compute true percent relative crrors to evaluate the accuracy of the trapezoidal approximations. 0

  • Write a MATLAB function/script that performs the following tasks. Approximate: 2+2 (a) Using the composite Trapezoidal...

    Write a MATLAB function/script that performs the following tasks. Approximate: 2+2 (a) Using the composite Trapezoidal rule with n=8 (b) Using the composite Simpson's rule with n = 8 (c) Display the final solution for each method along with the exact solution. Name your file: WS5_LastName_First Inital()

  • MATLAB Create a function that provides a definite integration using Simpson's Rule Problem Summar This example demo...

    MATLAB Create a function that provides a definite integration using Simpson's Rule Problem Summar This example demonstrates using instructor-provided and randomized inputs to assess a function problem. Custom numerical tolerances are used to assess the output. Simpson's Rule approximates the definite integral of a function f(x) on the interval a,a according to the following formula + f (ati) This approximation is in general more accurate than the trapezoidal rule, which itself is more accurate than the leftright-hand rules. The increased...

  • Question 1: Numerically integrate function f(x) given on the right from x=0 to x=10. Use the...

    Question 1: Numerically integrate function f(x) given on the right from x=0 to x=10. Use the f(x)= x2 - 6x + 16 Trapezoidal Rule and the Simpson's 1/3 Rule and compare the results. Use at least 4 50 + 15x - ye? decimal digits in your calculations and reporting. Organize and report each one of your solutions in a calculation table and identify your result clearly. a) Divide the interval into 5 subdivisions. Calculate the integral first using the Trapezoidal...

  • Use matlab please. Exercise 2 Use the functions you coded in Exercise 1 to compute the...

    Use matlab please. Exercise 2 Use the functions you coded in Exercise 1 to compute the numerical approximation of the integral .1 cos e 30 To this end, write a Matlab/Octave function function [en,et , es] test-integration() = that returns the following items: em, et, es: row vectors with components the absolute values of the integration errors llref-Inl n=2.3, . . . . 100 obtained with the midpont (vector em), trapezoidal (vector et) and Simpson (vector es) rules. Here, f...

  • 1. Numerical Integration The integral of a function f(x) for a s x S b can be interpreted as the ...

    1. Numerical Integration The integral of a function f(x) for a s x S b can be interpreted as the area between the f(x) curve and the x axis, bounded by the limits x- a and x b. If we denote this area by A, then we can write A as A-f(x)dx A sophisticated method to find the area under a curve is to split the area into trapezoidal elements. Each trapezoid is called a panel. 1.2 0.2 1.2 13...

  • Question 2. Consider the approximation of the definite integral () (a) Begin by using 2 points/nodes...

    Question 2. Consider the approximation of the definite integral () (a) Begin by using 2 points/nodes (i.e., n + 1 = 2, with the two points being x = a and r = b). Replace f(x) by the constant /(a+b)/2] on the entire interval a <<b. Show that this leads to the numerical integration formula M,()) = (b − a) ) Graphically illustrate this approximation. (b) In analogy with the derivation of the Trapezoidal rule and Simpson's rule, generalize part...

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