Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

function IntegralCalculator func= input('Enter an equation in x: ','s') ; f = str2func(['@(x) ' func]); a= input('Enter the bottom limit of the integral: '); b=

  • function IntegralCalculator

    func= input('Enter an equation in x: ','s') ;

    f = str2func(['@(x) ' func]);

    a= input('Enter the bottom limit of the integral: ');

    b= input('Enter the upper limit of the integral: ');

    n = input('Enter the number of nodes for the integral: ');

    method = input('Enter the method you want to use in capital letters: ', 's');

    int=0;

    while strcmp('LA',method)

    dx=(b-a)/n;

    r=f(a);

    for i = 1:n-1

    c=a+i*dx;

    r=r+f(c);

    end

    r=dx*r;

    nv=r;

    err= (nv-r)/nv;

    disp(["Your Left side estimate is",num2str(r)])

    disp(["Your approximated error is",num2str(err)])

    plot(1:n, r, '-', 'LineWidth', 2,'Color', 'b');

    return

    end

    while strcmp('RA', method)

    dx=(b-a)/n;

    w=f(a);

    for i = 1:n+1

    c=a+i*dx;

    w=w+f(c);

    end w=dx*w;

    disp(["Your Right side estimate is",num2str(w)])

    return

    end

    while strcmp('TR', method)

    d = linspace(a,b,n+1);

    v =d(2)-d(1);

    ls = sum(f(d(1:n))*v);

    rs = sum(f(d(2:n+1))*v);

    te = (ls +rs)/2;

    disp(["Your Trapezoidal estimate is",num2str(te)])

    return

    end

    while strcmp('MR', method)

    dx =(b-a)/n;

    for i= a:dx:b

    int = int + f(i+dx/2);

    end

    in =dx* int;

    disp (['Your Mid point estimate is: ', num2str(in)])

    return

    end

    while strcmp('3FD', method)

    dx =(b-a)/n;

    o=0;

    e=0;

    if rem(n,2)==1

    disp('ERROR! Enter a valid n')

    input("Enter n as an even number ")

    end

    for i =1:1:n-1

    s=f(a+(i*dx));

    o =o + f(s);

    end

    for i = 2:2:n-2

    s=f(a+(i*dx));

    e=e+f(s);

    end

    int = (dx/3) .* ((f(a)+f(b))+4 .*o + 2.*e);

    disp (['Your Simpson point estimate is: ', num2str(int)])

    return

    end

    end

    This is the code that I have. It is to use 5 different ways to approximate the calculation of an integral. It is working to do the approximations that I need to do. However, I need to calculate the error between the exact value and the approximation and plot the graph. I tried different things, but I haven't been able to get it to give me the error or the graph. I do not know what the function will be or the bounds for it because they have to be inputted by the user, so I do not have that information at the beginning. This is the last attempt that I tried but it did not work neither.


 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Numerical Analysis

Authors: Richard L. Burden, J. Douglas Faires

9th edition

538733519, 978-1133169338, 1133169333, 978-0538733519

More Books

Students also viewed these Programming questions