Please find the code below with detailed inline comments.
CODE
=======================
function I = simpson3_f ( f )
f = @(x) sqrt(1 - x*x)
% for calculating integrals using Simpson's 1/3 rule when function
is known
%asking for the range and desired accuracy
error = 1;
a = -1, b = 1;
%intial h and n
n = 100;
h = (b -a )/100;
%for calculating maximum of f''''(x) in the given region
for k = 0:100
x( 1, k+1 ) = a + k*h ;
y4( 1, k+1) = feval ( f, x(1,k+1) + 4*h ) - 4*feval( f, x(1,k+1) +
3*h )...
+ 6*feval( f, x(k+1) + 2*h ) - 4* feval ( f, x(1,k+1) + h )
...
+ feval( f, x(k+1) );% fourth order difference
end
[ y i ] = max( y4);
x_opt = x(1,i);
% for calculating the desired value of h for 1% error
m=0;
ddf = feval ( f, x_opt + 4*h ) - 4*feval( f, x_opt + 3*h )...
+ 6*feval( f, x_opt + 2*h ) - 4* feval ( f, x_opt + h ) ...
+ feval( f, x_opt );% fourth order difference
% dff defined outside bracket just for convinence
while ddf * ( b -a )/180 > error % error for Simpson's 1/3
rule
m = m +1;
h = h*10^-m;
ddf = feval ( f, x_opt + 4*h ) - 4*feval( f, x_opt + 3*h )...
+ 6*feval( f, x_opt + 2*h ) - 4* feval ( f, x_opt + h ) ...
+ feval( f, x_opt );% defined here for looping
end
%calculating n
n = ceil( (b-a)/h );
if rem( n,2) == 0
n = n+1;
end
h = ( b-a )/n;
% calculating matrix X
for k = 1:(n+1)
X(k,1) = a + (k-1)*h;
X(k,2) =feval ( f, X(k,1));
end
%calculating integration
i= 1; I1 = 0;
while ( 2*i ) < (n+1)
I1 = I1 + X ( ( 2*i) , 2 );
i = i +1;
end
j=1; I2 =0;
while (2*j + 1) < (n+1)
I2 = I2 + X ( ( 2*j + 1) , 2);
j = j + 1;
end
I = h/3 * ( X( 1,2) + 4*I1 + 2*I2 + X(n,2) );% Simpson's 1/3
rule
% Display final result
h
n
disp(sprintf(' Using this integration for the given function in the
range ( %10f , %10f ) is %10.6f .',a,b,I))
OUTPUT
======================
10 points (done by MATLAB:code submission required) It is known that -1 (a) Write a MATLAB...
Numerical Methods
Consider the integral 2 (a) [16 marks] Use the composite Simpson's rule with four intervals to calculate (by hand) approximate value of the integral Calculate the maximum value of the error in your approximation, and compare it with the true error. (b) 19 marks] Determine the number of subintervals n and the step size h so that the composite Simpson's rule for n subintervals can be used to compute the given integral with an accuracy of 5 ×...
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...
Write the program in Python!
We want to evaluate the integral integral_0^1 e^-t^2 dt. Write a Python (or C, etc.) function that computes the integral above using the composite Simpson method with N subintervals of [0, 1]: here N is the input parameter. We assume N lessthanorequalto 200. Run this program and print the output when N = 10, 50, 100. Use the error formula of the composite Simpson method to find an upper bound on the error for this...
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...
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)...
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...
matlab help plz
Overview: In this exercise, you will write code to compare how two different mumerical methods (a middle Riemann sum, and MATLAB's integral function) evaluate the function fx) and the x-axis. The code should output the error between the two calculated areas. area between a Function Inputs Func- the function to be numerically integrated. a-the lower interval value. b-the upper interval value. N-the number of rectangles to be used. Function Outputs: Area Riemann- the numerical approximation for the...
Exercise 6: Given the table of the function f(x)-2" 2 X 0 3 2 f(x) 1 2 4 8 a) Write down the Newton polynomials P1(x), P2(x), Pa(x). b) Evaluate f(2.5) by using Pa(x). c) Obtain a bound for the errors E1(x), E2(x), Es(x) Exercise 7: Consider f(x)- In(x) use the following formula to answer the given questions '(x) +16-30f+16f,- 12h a) Derive the numerical differentiation formula using Taylor Series and find the truncation error b) Approximate f'(1.2) with h-0.05...
PLEASE use MATLAB only to write the code
100 10 Assume -18.79829683678703 is the "exact" value of the integral. Estimate this integral by the composite trapezoid rule with 3, 5, and 7 points. Calculate the percent difference from the exact value in each case using PD-100 abs(exact-trap)Vexact)
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...