Question

In: Advanced Math

Write matlab program to compute ∫f(x)dx lower bound a upper bound b using simpson method and...

Write matlab program to compute ∫f(x)dx lower bound a upper bound b using simpson method and n points. Then, by recomputing with n/2 points and using richardson method, display the exact error and approximate error. (Test with a=0 b=pi f(x)=sin(x))

Solutions

Expert Solution


%%Matlab code for finding integration using 4 different method
clear all
close all
%functions for which integration have to do
fun1=@(x) sin(x);

%Number of steps in each methods
N=40;
%Limit of integration for function
a1=0; b1=pi;

fprintf('\n\nFor the function f(x)= ')
disp(fun1)
%Exact integration for function 1
I_ext = integral(fun1,a1,b1);
fprintf('\nExact integration value for a=%f to b=%f is %f.\n',a1,b1,I_ext)

%Simpson integration for function 1
I_smp1=simpson(fun1,a1,b1,N);
fprintf('\n\tSimpson integration for function 1 for N=%d is %f.\n',N,I_smp1)
fprintf('Error in Simpson integration for n=%d is %e.\n',N,abs(I_ext-I_smp1))

I_smp2=simpson(fun1,a1,b1,N/2);
fprintf('\n\tSimpson integration for function 1 for N=%d is %f.\n',N/2,I_smp2)
fprintf('Error in Simpson integration for n=%d is %e.\n',N/2,abs(I_ext-I_smp2))

%Integration value using Richardson extrapolation
I_rchrd=I_smp1+(I_smp1-I_smp2)/3;
fprintf('\n\tIntegration value using Richardson extrapolation is %f.\n',I_rchrd)
fprintf('Error in Richardson integration is %e.\n',abs(I_rchrd-I_ext))
%%Matlab function for Simpson integration
function val=simpson(func,a,b,N)
    % func is the function for integration
    % a is the lower limit of integration
    % b is the upper limit of integration
    % N number of rectangles to be used
    %splits interval a to b into N+1 subintervals
    xx=linspace(a,b,N+1);
    dx=xx(2)-xx(1); %x interval
    aa=func(xx(1));
    val=(dx/3)*(double(aa)+double(func(xx(end))));
    %loop for Riemann integration
        for i=2:length(xx)-1
            xx1=xx(i);
            if mod(i,2)==0
                val=val+(dx/3)*4*double(func(xx1));
            else
                val=val+(dx/3)*2*double(func(xx1));
              
            end
        end    
end

%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%


Related Solutions

Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
Given the integral 1/x dx upper bound 2 lower bound 1 (a) use simpson's rule to...
Given the integral 1/x dx upper bound 2 lower bound 1 (a) use simpson's rule to approximate the answer with n=4 Formula:f(x)=1/3[f(x0)+4f(x1)+2f(x2)+...+f(xn)]Δx(keep answer to 6 decimals) b)how large is n in order for the error of Simpsons rule for the given integral is no more than 0.000001 Formula: |Es|=(k)(b-a)^5/(180 n^4), where |f^4(x)≤k| please show all work and steps
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (curve fit), and x for f(x).
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (linear, quadratic, or cubic), and x for f(x).
(a) State the definition of the derivative of f. (b) Using (a), prove the following:d/dx(f(x) +g(x))...
(a) State the definition of the derivative of f. (b) Using (a), prove the following:d/dx(f(x) +g(x)) =d/dx(f(x)) +d/dx(g(x))
Computing3√25 using MATLAB. (a) Beginning with the interval [2,3], and f(x) =x^3−25, use the error bound...
Computing3√25 using MATLAB. (a) Beginning with the interval [2,3], and f(x) =x^3−25, use the error bound for bisection to predict how many iterates bisection would need to get error within 10^−20. (b) Run bisection on this problem. How many iterations did it need? For some of the iterates compute the absolute error. What is happening approximately to the number of significant digits of accuracy with each iteration? (c) Write a program to perform Newton’s method on f(x) =x^3−25 with p0=...
Refer to f(x)=1/x^2 1)Give an upper bound for the error you get when using the fourth...
Refer to f(x)=1/x^2 1)Give an upper bound for the error you get when using the fourth degree Taylor polynomial centered at c = 2 to approximate f(2.1) 2)What is the actual error of your approximation? (Use a calculator.)
Assuming that the variables a, b, c, d, and f are scalars, write MATLAB statements to compute and display
Assuming that the variables a, b, c, d, and f are scalars, write MATLAB statements to compute and display the following expressions. Test your statements for the values a = 1.12, b = 2.34, c = 0.72, d = 0.81, and f = 19.83.
Write a program to compute the root of the function f(x) = x3 + 2 x2...
Write a program to compute the root of the function f(x) = x3 + 2 x2 + 10 x - 20 by Newton method ( x0 =2 ). Stop computation when the successive values differ by not more than 0.5 * 10-5 . Evaluate f(x) and f '(x) using nested multiplication. The output should contain: (1) A table showing at each step the value of the root , the value of the function,and the error based upon successive approximation values...
Find the Upper and Lower Darboux sums for the following functions. (i) f(x) = −x −...
Find the Upper and Lower Darboux sums for the following functions. (i) f(x) = −x − 1 on [0, 3], n = 3. [10] (ii) f(x) = 1 + 2x 0n [0, 1] , n = 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT