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 simpsons estimate
end
------
%MATLAB Code for the function:
function [value, error] = trapezoidal(func, a, b, n, I) % returns the value and error
ret = 0;
h = (b-a)/(n+1); %step size
pts = a:h:b; %array of points
for i=2:n+2
ret = ret + (func(pts(i)) + func(pts(i-1)))*h/2;
end
value = ret;
error = abs(I - value)*100/I; %error between real value and trapezoidal estimate
end
The program is interactive and should include the following:
Ask the user to input which rule to use (e.g. `1’ for composite trapezoidal and `2’ for composite Simpson’s 1/3 rule).
Ask the user to input the number of points for which the integration is calculated. The program should notify the user if an invalid number of points are used.
Print the percentage error between the approximated solution and the solution obtained through the quad function.
Code in matlab:
Output:
%-----------------------Code---------------------
% test function takes func, a, b, I as parameters,
% n=number of points is taken as input when test() is called.
function [value, error]=test(func, a, b, I)
n=input('Enter number of points: '); % number of points
if(n<1) % checks the validity of n, if false calls the function
again
disp('Please enter valid number of points: ')
test(func, a, b, I);
end
% option to select either 1. trapezoidal or 2. simpsons.
option=input('Enter an option: 1.> trapezoidal and 2.>
simpsons:');
I_quad=quad(func, a, b); % integrates func from x=a to b using quad
function
if(option==1)
[value, error]=trapezoidal(func, a, b,n, I);
% calculates percentage error for value and I_quad
fprintf('Percentage error when using quad function: %f',
abs(value-I_quad)*100/value);
elseif(option==2)
[value, error]=simpsons(func, a, b,n, I);
% calculates percentage error for value and I_quad
fprintf('Percentage error when using quad function: %f',
abs(value-I_quad)*100/value);
else % if option is neither 1 nor 2 then call function again
disp('Please select valid option');
test(func, a, b, I);
end
end
% simpsons method to integrate fuction func
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 simpsons
estimate
end
function [value, error] = trapezoidal(func, a, b,n, I) % returns
the value and error
ret = 0;
h = (b-a)/(n+1); %step size
pts = a:h:b; %array of points
for i=2:n+2
ret = ret + (func(pts(i)) + func(pts(i-1)))*h/2;
end
value = ret;
error = abs(I - value)*100/I; %error between real value and
trapezoidal estimate
end
MATLAB Write an m-file capable of performing numerical integration for the equation using the simpsons and...
Use Matlab code Consider the following function sin(x) 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 I:Integral estimate a) Write a function capable of performing numerical integration of h(x) using the composite trapezoidal rule. Use your function to integration the equation with 9 points. Write a function capable of performing numerical integration of h(x) using the...
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...
Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code below? %%Matlab code for Question 5. syms X y intlx exp(x),0,2); %integration of x*exp(x) fprintf("htlntegration of x"2*exp(x) for [O 3] is %2.5f.\n,y) %Integration using Trapizoidal rule nn [100 1000]; for ii 1:2 a:0; b-3; %Integration limit ns-1:nn(i) integral Values-zeros(size(ns)); ourFunction-@(x) x.*2.*exp(x); for n-ns val(n)-trapizoidal (ourFunction,a,b,nn(i); end fprintf nlntegration of x 2*exp(x) using Trapizoidal rule for [O 3]\n') fprintf('1tTrapizoidal integration value for n-%d...
Q1: The error function erf(z) is defined as: Using integration step of 0.05, evaluate erf(0.5) and compare with the tabulated value erf(0.5)=0.5205 using: Three-point Gaussian quadrature formula Trapezoidal Rule (h=0.05) Simpson’s 1/3 Rule Simpson’s 3/8 Rule Compare the results. Note: change the step to suit the assigned methods that are restricted by the number of panels. That is, increase the number of panels to a level that enables you to use the method(s) that cannot be used at the integration...
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...
2 Problem 3 (25 points) Let I = ïrdz. a) [by hand] Use a composite trapezoidal rule to evaluate 1 using N = 3 subintervals. b) MATLAB] Use a composite trapezoidal rule to evaluate I using N - 6 subinterval:s c) by hand] Use Romberg extrapolation to combine your results from a) and b) and obtain an improved approximation (you may want to compare with a numerical approximation of the exact value of the integral 2 Problem 3 (25 points)...
(a) (4 points) Fill in the blanks in the following MATLAB function M file trap so that it implements the composilu trapezidul rulo, using a soquence of w -1,2, 4, 8, 16,... trapezoids, to approximatel d . This M file uses the MATLAB built-in function trapz. If 401) and y=() f(!)..... fr. 1)). 3= ($1, then the execution of z = trapz(x, y) approximates using the composite trapezoidal rule (with trapezoids). (Note that the entries of are labeled starting at...
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()
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...
Help PLEASE! Project. Approximating In 2 and π by numerical integration. 1. In Calculus 1 you learned that the natural logarithm of the number 2 is the value of the integral da The value of In 2 correct to 15 decimal places is In2 0.693147180559945 Use the error formula for the Composite Simpson's rule to determine how many correct decimal places you can obtain when you partition [1,2] into 4, n 8, and12 equal-length subintervals. 2. In Calculus you learned...