Question

These instructions are written with the assumption that code will be done in matlab. You might fi...

These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max.

2. Numerical Integration (Quadrature). Write FOUR of your own numerical integration routines. One should use left-end, right-end, or mid-points, another should use the trapezoid method, another should use Simpson’s method, and the fourth should use either Guassian Quadrature or Romberg’s method. Use your four routines to approximate two integrals numerically so that they are accurate to 1 part in 1 billion (if possible, if this is not possible explain why). First use your routines to approximate f(x) = x 3 − x cos(x) on the interval [0, 2π], you can and should check that your answer is correct. Then use your routines to approximate f(x) = 4e −2x 2 on the interval [−1, 1]. Use one of matlab’s built in integration functions to approximate both integrals also. Each integral should be: numerically approximated FOUR ways, checked vs one of matlab’s built-in numerical integration methods, and the polynomial/trig function should be checked exactly.

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


Matlab code for finding integration using 4 different method clear all close all %functions for which integration have to doI rmn-left riemann(fun2, a2,b2,N); fprint f ( .\n\ tLeft Riemann integration for function 2 for N-%d is %f. n,N, I rmn) tprixx 1 =xx (1); val-val+dx*double (func (xxl)); end val-val+dx* (0.5*double (func (xx(1)))+0.5*double (func (xx (end)))) %%Matl용옹옹%%옹옹%%옹옹%%옹옹%%옹옹 End of Code 용융 응용 For 1st function f (x)- e (x)x. 3-x. *cos (x) Exact integration value for a-0.000000 to

%%Matlab code for finding integration using 4 different method
clear all
close all
%functions for which integration have to do
fun1=@(x) x.^3-x.*cos(x) ;
fun2=@(x) 4.*exp(-2.*x.^2) ;
%Number of steps in each methods
N=100;
%Limit of integration for function 1.
a1=0; b1=2*pi;
%Limit of integration for function 2.
a2=-1; b2=1;

fprintf('\n\nFor 1st 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)

%Left Riemann integration for function 1
I_rmn=left_riemann(fun1,a1,b1,N);
fprintf('\n\tLeft Riemann integration for function 1 for N=%d is %f.\n',N,I_rmn)
fprintf('Error in Left Riemann integration is %e.\n',abs(I_ext-I_rmn))

%Trapizoidal integration for function 1
I_trp=trapizoidal(fun1,a1,b1,N);
fprintf('\n\tTrapizoidal integration for function 1 for N=%d is %f.\n',N,I_trp)
fprintf('Error in Trapizoidal integration is %e.\n',abs(I_ext-I_trp))

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

%Romberg integration for function 1
I_rom=romberg(fun1,a1,b1,10);
fprintf('\n\tRomberg integration for function 1 for N=%d is %f.\n',10,I_rom)
fprintf('Error in Romberg integration is %e.\n',abs(I_ext-I_rom))

fprintf('\n\nFor 2nd function f(x)= ')
disp(fun2)
%Exact integration for function 2
I_ext = integral(fun2,a2,b2);
fprintf('\nExact integration value for a=%f to b=%f is %f.\n',a2,b2,I_ext)

%Left Riemann integration for function 2
I_rmn=left_riemann(fun2,a2,b2,N);
fprintf('\n\tLeft Riemann integration for function 2 for N=%d is %f.\n',N,I_rmn)
fprintf('Error in Left Riemann integration is %e.\n',abs(I_ext-I_rmn))

%Trapizoidal integration for function 2
I_trp=trapizoidal(fun2,a2,b2,N);
fprintf('\n\tTrapizoidal integration for function 2 for N=%d is %f.\n',N,I_trp)
fprintf('Error in Trapizoidal integration is %e.\n',abs(I_ext-I_trp))

%Simpson integration for function 2
I_smp=simpson(fun2,a2,b2,N);
fprintf('\n\tSimpson integration for function 2 for N=%d is %f.\n',N,I_smp)
fprintf('Error in Simpson integration is %e.\n',abs(I_ext-I_smp))

%Romberg integration for function 2
I_rom=romberg(fun2,a2,b2,10);
fprintf('\n\tRomberg integration for function 2 for N=%d is %f.\n',10,I_rom)
fprintf('Error in Romberg integration is %e.\n',abs(I_ext-I_rom))

%%Matlab function for Left Riemann integration
function val=left_riemann(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
    val=0;
    %splits interval a to b into N+1 subintervals
    xx=linspace(a,b,N+1);
    dx=xx(2)-xx(1); %x interval
    %loop for Riemann integration
        for i=1:length(xx)-1
            xx1=xx(i);
            val=val+dx*double(func(xx1));
        end
end

%%Matlab function for Trapizoidal integration
function val=trapizoidal(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
    val=0;
    %splits interval a to b into N+1 subintervals
    xx=linspace(a,b,N+1);
    dx=xx(2)-xx(1); %x interval
    %loop for Riemann integration
        for i=2:length(xx)-1
            xx1=xx(i);
            val=val+dx*double(func(xx1));
        end
       val=val+dx*(0.5*double(func(xx(1)))+0.5*double(func(xx(end))));
end

%%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
    val=(dx/3)*(double(func(xx(1)))+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

%%Matlab function for Romberg integration
function val = romberg(func,a,b,N)
% f - Matlab inline function
% a,b - integration interval
% n - number of rows in Romberg tableau
% toll- tollerence of 2 succesive romberg value
% Output:
% r - Romberg tableau containing the computed values of the integral
% h - step length
% n - size of Romberg table
        h = (b - a) ./ (2.^(0:N-1));
        r(1,1) = (b - a) * (func(a) + func(b)) / 2;
        for j = 2:N
            subtotal = 0;
            for i = 1:2^(j-2)
                subtotal = subtotal + func(a + (2 * i - 1) * h(j));
            end
            r(j,1) = r(j-1,1) / 2 + h(j) * subtotal;
            for k = 2:j
                r(j,k) = (4^(k-1) * r(j,k-1) - r(j-1,k-1)) / (4^(k-1) - 1);
            end
        end
        val=r(end,end);
end


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

Add a comment
Know the answer?
Add Answer to:
These instructions are written with the assumption that code will be done in matlab. You might fi...
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
  • These instructions are written with the assumption that code will be done in matlab. You might fi...

    These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max. 1. A spring-mass system has the following position y(t) and velocity v(t) functions: y(t) = e −0.5t sin(2t) + √ 3e −0.5t cos(2t) v(t) = e −0.5t (2√ 3 − 0.5) sin(2t) − 0.5(4 + √ 3)e −0.5t cos(2t) where the units are in...

  • Explain using Matlab code but also why you used the linear system please 1 Quadrature Rule...

    Explain using Matlab code but also why you used the linear system please 1 Quadrature Rule A quadrature rule is a way to approximate integrals numerically i.e. using a computer). Many such quadrature rules can be derived by solving a simple linear system. Set up a linear system and then use Matlab to find the coefficients wo, W1, W2, W3, W4, W5 such that | f(x)dx = wof(0) + wif(0.2) + w2f(0.4) + w3f(0.6) +w4f(0.8) + w5f (1) for each...

  • Programming Assignment - Numerical Integration In MATH 1775 last semester, you used Riemann sums in Excel...

    Programming Assignment - Numerical Integration In MATH 1775 last semester, you used Riemann sums in Excel to approximate the value of the definite integral that represents the area of the region between the x-axis and the graph of 2 5 y xx = − 8 shown below. The purpose of this assignment is to increase the accuracy of such approximations of integrals. Write a program (using java) that uses the Midpoint Rule, the Trapezoid Rule, and Simpson’s Rule to find...

  • i need the coding in matlab 1) Numerical Integration There are many simply written functions which...

    i need the coding in matlab 1) Numerical Integration There are many simply written functions which have no simple closed form antiderivative. These functions must, when they arise in physical problems, be integrated numerically. One such function is f(x) = sin(x) a) Find S f(x)dx using the trapezoid rule and 2 sub-intervals b) Repeat part a) with 10 sub-intervals c) Find S, f(x)dx using Simpson's rule and 2 sub-intervals d) Repeat part c) with 10 sub-intervals e) Find S, f(x)dx...

  • For the final project, you will create an integral calculator polynomials of degree zero, one, two...

    For the final project, you will create an integral calculator polynomials of degree zero, one, two three and four Degree Function yz where z is a constant 3 where a,b.c.d.z e R, real numbers You will ask the user for the degree of the polynomial and then the coefficients and the constant values of the polynomial. Then you will ask the user for the beginning and the end of the interval along with the number of steps between the interval...

  • Im not sure if this site uses MATLAB, but ill post the question anyway. MidPoint Rule...

    Im not sure if this site uses MATLAB, but ill post the question anyway. MidPoint Rule In this phase, we will evaluate the integral numerically using the definition by Riemann sum. For numerical calculations, we will use MATLAB software 3. First, use MATLAB to evaluate this time a definite integral x ехах For that, type directly into command window in MATLAB: syms x; int(x*exp(x),0,2). Get the answer in a number with at least four decimals. . Download an m-file, midPointRule.m,...

  • Matlab code: Task 5 As an engineer you are asked to calculate the approximate surface area of a lake in your local community. In order to do that, you decide to use a GPS to measure the x and y coord...

    Matlab code: Task 5 As an engineer you are asked to calculate the approximate surface area of a lake in your local community. In order to do that, you decide to use a GPS to measure the x and y coordinates at various points around the lake. You pick a starting point and calibrate your GPS position at that point to be (0,0). You begin to walk around the lake collecting the following data below and eventually return to your...

  • the code in the photo for this I.V.P dy/dx= x+y. y(0)=1 i need the two in the photo thank you New folder Bookmark...

    the code in the photo for this I.V.P dy/dx= x+y. y(0)=1 i need the two in the photo thank you New folder Bookmarks G Google dy/dx x+y, y(0)=1 2 h Exact Solution 1.8 Approximate Solution Mesh Points 1.6 -Direction Fied 1.4 1.2 1 0.8 04 0.2 0.3 0.1 0 X CAUsersleskandara\Desktop\New folder emo.m EDITOR PUBLISH VEW Run Section FILE NAVIGATE EDIT Breakpoints Run Run and FL Advance Run and Advance Time BREAKPOINTS RUN 1 - clear all 2 clc 3-...

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

  • Submission Items: (1) A Word document in which you: a. Describe in brief the problem you...

    Submission Items: (1) A Word document in which you: a. Describe in brief the problem you are solving and the numerical method used. b. Snapshot of the output, when you run the program for the conditions mentioned in the questions. c. Discuss your results. (2) MATLAB code (m) file() Question 1: [10 points) Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan(x) = x- + +... The function should accept 3 parameters:...

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
Active Questions
ADVERTISEMENT