Question

Write a MATLAB function that uses Simpson’s 1/3 method ( to integrate any function) to estimate...

Write a MATLAB function that uses Simpson’s 1/3 method ( to integrate any function) to estimate an even
more accurate value of the integral using Richardson extrapolation. The function
should have the following inputs: the function name, the integration range, and the
number of intervals. It should output as an argument the numerical value of the
integral. The function should stop with an error message if an inappropriate number
of intervals are requested.

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

ONLY SIMPSON:

Matlab Function file:(my_simps13.m)

function intVal=my_simps13(foo,a,b,N)
% h is step size
h=(b-a)/N;
X=zeros(1,N+1);
Y=zeros(1,N+1);
for i=1:N+1
X(i)=a+(i-1)*h;
Y(i)=foo(X(i));
end
if rem(N,2)~=0
error('No. of segments must be in multiples of 2');
end
% I=h/3{(y0+yn)+4*(y1+y3+y5+y7+.......yn-1)+2*(y2+y4+y6+y8+....yn-1)}
% As the indices of vector in Mat Lab starts from 1
% yk is termed as yk+1 Example: y0 is termed as y1 and so on....
intVal=0;
for i=2:2:N
intVal=intVal+(4*h/3*(Y(i)));
end
for i=3:2:N
intVal=intVal+(2*h/3*(Y(i)));
end
intVal=intVal+((h/3)*(Y(N+1)+Y(1)));
end

Screenshot:

Save the above program as my_simps13.m in the working directory.

Matlab Script file:

clear;
clc;
% lower limit
a=0;
% upper limit
b=10;
% no. of intervals
N=10;
% function is ananymous so @ is not required
foo=@(x) 4*x+x.^3;
intVal=simpson_int(foo,a,b,N)

Screenshot:

Save the above program in the working directory and execute it.

Note:

Yoy can copy the code in command window also, saving script file is not needed

Result:

RICHARDSON INCLUDING:

Matlab Function file:(my_simps13.m)

function [intVal_simp,intVal_Rich]=my_simps13(foo,a,b,N)
% h is step size
h=(b-a)/N;
X=zeros(1,N+1);
Y=zeros(1,N+1);
for i=1:N+1
X(i)=a+(i-1)*h;
Y(i)=foo(X(i));
end
if rem(N,2)~=0
error('No. of segments must be in multiples of 2');
end
% I=h/3{(y0+yn)+4*(y1+y3+y5+y7+.......yn-1)+2*(y2+y4+y6+y8+....yn-1)}
% As the indices of vector in Mat Lab starts from 1
% yk is termed as yk+1 Example: y0 is termed as y1 and so on....
intVal_simp=0;
for i=2:2:N
intVal_simp=intVal_simp+(4*h/3*(Y(i)));
end
for i=3:2:N
intVal_simp=intVal_simp+(2*h/3*(Y(i)));
end
intVal_simp=intVal_simp+((h/3)*(Y(N+1)+Y(1)));
% I=h/4{(y0+yn)+2*(y1+y3+y5+y7+.......yn-1)+2*(y2+y4+y6+y8+....yn-1)}
% As the indices of vector in Mat Lab starts from 1
% yk is termed as yk+1 Example: y0 is termed as y1 and so on....
intVal_Rich=0;
for i=2:2:N
intVal_Rich=intVal_Rich+(2*h/3*(Y(i)));
end
for i=3:2:N
intVal_Rich=intVal_Rich+(2*h/3*(Y(i)));
end
intVal_Rich=intVal_Rich+((h/4)*(Y(N+1)+Y(1)));
end

Screenshot:

Save the above program as my_simps13.m in the working directory.

Matlab Script file:

clear;
clc;
% lower limit
a=0;
% upper limit
b=10;
% no. of intervals
N=10;
% function is ananymous so @ is not required
foo=@(x) 4*x+x.^3;
% exact value is 2700
EV=2700;
[intVal_simp,intVal_Rich]=my_simps13(foo,a,b,N)
fprintf('Error in Simpson1/3 is %.2f percntage\n',abs((EV-intVal_simp)/EV)*100);
fprintf('Error in Richardson is %.2f percentage\n',abs((EV-intVal_Rich)/EV)*100);

Screenshot:

Save the above program in the working directory and execute it.

Note:

Yoy can copy the code in command window also, saving script file is not needed

Result:

I hope this will help you, please give me thumbs-up.

Add a comment
Know the answer?
Add Answer to:
Write a MATLAB function that uses Simpson’s 1/3 method ( to integrate any function) to estimate...
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...

  • 1.1) Write a script in MATLAB that uses Reimann sum with 100 intervals (n=100) to calculate...

    1.1) Write a script in MATLAB that uses Reimann sum with 100 intervals (n=100) to calculate hydrostatic force of water on a triangular gate submerged under water (Figure 1). Give two estimates of this force (lower and upper bound for value of force). Hint: You may need to first find the hydrostatic force on the thin hashed slice by hand using the following information. Hydrostatic pressure P = pgz. p = 10003,= 9.81 m2/s. Note that h = 7-z and...

  • Use Matlab code Consider the following function sin(x) Using the following parameters in your functions: -func:...

    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 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...

  • matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name...

    matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name the function [max_value, max_index] - LASTNAMES matrix max (x), where input argument x is a vector or matrix (not a scalar), and the outputs are the maximum value(s) and indexes of the maximum value(s) of the mput x. Do not use MATLAB functions max() or find(). Use loops and if branches to determine the maximum elements of matrix. Check for correct number and valid...

  • 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...

  • matlab help plz Overview: In this exercise, you will write code to compare how two different mumerical methods (a mi...

    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...

  • 8. Write a MATLAB function that will take as inputs a set of data (x,y) and...

    8. Write a MATLAB function that will take as inputs a set of data (x,y) and output the best estimate of the integral using a Romberg Array. The function should perform two trapezoidal integrations using the full interval and half the interval, and then combine these integrations in a Romberg Array. Your function should also output the relative error of the integral. Please note that the data entered may not be evenly spaced and your function should make sure that...

  • 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...

  • Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...

    Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root ^n and the number of steps n taken to reach the solution. Use function mynewton.m to perform...

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