Question

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

In this phase, we will evaluate the integral numerically using the definition by Riemann sum. For numerical calculations, we

MidPoint Rule

% remember, we are calculating integral from 0 to 2 of xe ^x dx % integration limits b=2 ; ns=1 : 100; integralValues-zeros (

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

SMatlab code for Question 3. clear all close all syms x y-int(x*exp(x),0,2); tintegration of x*exp(x) fprint f(\tIntegrationend %%Matlab code for Question 5. syms x int(Xexp ( x),0,2); Y- tintegration of x*exp (x) fprint f (\tIntegration of x^2*exIntegration of x*exp(x) for [0 2j is 8.38906. Integration of x*exp(x) using Midpoint rule for [0 2 Mİdpoint integration valueApproximation by midpoint rule for n-1000 8.5 7.5 ntegral approximatian 8.3891 6.5 5.5 5 100 200 300 600 00 01000 Published w

%%Matlab code for Question 3.
clear all
close all

syms x
y=int(x*exp(x),0,2);

%integration of x*exp(x)
fprintf('\tIntegration of x*exp(x) for [0 2] is %2.5f.\n',y)

%%Matlab code for Question 4.
%Integration using Midpoint rule

nn=[100 1000];

%loop for 2 different step size

for ii=1:2
  
    a=0; b=2; %Integration limit
    ns=1:nn(ii);
    integralValues=zeros(size(ns));
    ourFunction=@(x) x.*exp(x);

    for n=ns
        deltaX=(b-a)/n;
        xs=a+(0:n)*deltaX;
        xsBar=(xs(1:end-1)+xs(2:end))/2;
        f=ourFunction(xsBar);
        integralValues(n)=deltaX*sum(f);
    end

    %%Drawing figure
    figure(ii)
    plot(ns,integralValues,'Linewidth',2)
    title(sprintf('Approximation by midpoint rule for n=%d',ns(end)))
    xlabel('n')
    ylabel('Riemann sum')

    dim=[.6 .4 .9 .3];
    str=['integral approximation ',num2str(integralValues(end))];
    annotation('textbox',dim,'String',str,'FitBoxToText','on');
    xa=[0.7 0.89];
    ya=[0.7 0.89];
    annotation('arrow',xa,ya)
    ax=gca;
    grid
  
    fprintf('\nIntegration of x*exp(x) using Midpoint rule for [0 2]\n')
    fprintf('\tMidpoint integration value for n=%d is %f .\n',nn(ii),integralValues(end))
end


%%Matlab code for Question 5.

syms x
y=int(x*exp(x),0,2);
%integration of x*exp(x)
fprintf('\tIntegration of x^2*exp(x) for [0 3] is %2.5f.\n',y)

%Integration using Trapizoidal rule
nn=[100 1000];

for ii=1:2
  
    a=0; b=3; %Integration limit
    ns=1:nn(ii);
    integralValues=zeros(size(ns));
    ourFunction=@(x) x.^2.*exp(x);

    for n=ns
        val(n)=trapizoidal(ourFunction,a,b,nn(ii));
    end
  
    fprintf('\nIntegration of x^2*exp(x) using Trapizoidal rule for [0 3]\n')
    fprintf('\tTrapizoidal integration value for n=%d is %f .\n',nn(ii),val(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


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

Add a comment
Know the answer?
Add Answer to:
Im not sure if this site uses MATLAB, but ill post the question anyway. MidPoint Rule...
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
  • T-Mobile 5:33 PM < Back MATH 1620 Pr… aビ Phase 2 In this phase, we will evaluate the integral nu...

    T-Mobile 5:33 PM < Back MATH 1620 Pr… aビ Phase 2 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 ex dx = For that, type directly into command window in MATLAB: syms x; intx exp(x),0,2). Get the answer in a number with at least four decimals. Download an m-file, midPointRule.m, from Canvas and...

  • Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code be...

    Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code below? %%Matlab code for Question 5. syms X y intlx exp(x),0,2); %integration of x*exp(x) fprintf("htlntegration of x"2*exp(x) for [O 3] is %2.5f.\n,y) %Integration using Trapizoidal rule nn [100 1000]; for ii 1:2 a:0; b-3; %Integration limit ns-1:nn(i) integral Values-zeros(size(ns)); ourFunction-@(x) x.*2.*exp(x); for n-ns val(n)-trapizoidal (ourFunction,a,b,nn(i); end fprintf nlntegration of x 2*exp(x) using Trapizoidal rule for [O 3]\n') fprintf('1tTrapizoidal integration value for n-%d...

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

  • EXAMPLE 5 Use the Midpoint Rule with n = 5 to approximate the following integral. dx...

    EXAMPLE 5 Use the Midpoint Rule with n = 5 to approximate the following integral. dx х SOLUTION The endpoints of the subintervals are 1, 1.6, 2.2, 2.8, 3.4, and 4, so the midpoints are 1.3, 1.9, 2.5, 3.1, and width of the subintervals is Ax = (4 - 175 so the Midpoint Rule gives The 1.9* 2s 313) dx Ax[f(1.3) + (1.9) + (2.5) + F(3.1) + f(3.7)] -0.06 2 + 1.3 2.5 3.1 . (Round your answer to...

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

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

  • I DESPERATELY NEED HELP WITH THIS DIFFERENTIAL EQUATIONS MATLAB ASSIGNMENT IM SUPPOSED TO BE LEARNING BUT...

    I DESPERATELY NEED HELP WITH THIS DIFFERENTIAL EQUATIONS MATLAB ASSIGNMENT IM SUPPOSED TO BE LEARNING BUT WE HAVE A SUB AND HE DIDN'T TEACH IT! ITS EULER AND IMPROVED EULER IN MATLAB! HERE IS THE LINK FOR THE IMAGE FILE THAT SHOWS THE FULL INSTRUCTIONS FOR THE CODE. https://imgur.com/a/gjmypLs Also, here is my code so far that I borrowed form an old assignment but the data is all wrong and the application of the code is slightly different so either...

  • MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts...

    MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts in order to get full credit. Help me out, thank you f LAB SECTION NAME HW6P3 (30 points) following are infinite series representations of the function, un pra i a script file HW6P3 that determines the value of the function from the sum of the series for a given value of x. The program (1 pt) must prompt the user to enter a value...

  • MATLAB Notes: B) Find likelihood for each sequence 1-9 for each hypothesis. First hypothesis with the...

    MATLAB Notes: B) Find likelihood for each sequence 1-9 for each hypothesis. First hypothesis with the fair coin (theta value = 0.5): what is the probability that we can get each sequence, 1 through 9. Second hypothesis with weighted coin(don’t know theta value): what is likelihood of getting sequence 1 through 9. Using equation to find all possible hundred theta values. For each theta value we need to compute likelihood. Then we sum them up across all possible theta values....

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
ADVERTISEMENT