Question

T-Mobile 5:33 PM < Back MATH 1620 Pr… aビ Phase 2 In this phase, we will evaluate the integral numerically using the definitio

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


Matlab code for integration clear all close all exact integration syms x I_ext-int (x*exp (x),0,2); function for which integrI_trap-Trapizoidal_int (f,a,b,n); checking wheather it correct upto two decimal or not if abs(I_trap-I_ext)<-10A-3 fprintf InFor the function having 1imit a-0.000000 to b-2.000000, f(x)- « (x)x. *exp(x) Exact Integration value for the function is 8.3

%%Matlab code for integration
clear all
close all

%exact integration
syms x
I_ext=int(x*exp(x),0,2);

%function for which integration have to do
f=@(x) x.*exp(x);
%upper and lower limit
a=0;b=2;
%displaying the function
fprintf('\nFor the function having limit a=%f to b=%f, f(x)=',a,b)
disp(f)
fprintf('\tExact Integration value for the function is %f.\n',I_ext);


%Integration using midpoint rule to get approximation correct upto two
%decimals

for n=1:1000
  
    I_mid=Midpoint_int(f,a,b,n);
    %checking wheather it correct upto two decimal or not
    if abs(I_mid-I_ext)<=10^-3
      
        fprintf('Integration value for the function using Midpoint rule is %f.\n',I_mid);
        break
      
    end
end


syms x
I_ext=int(x^2*exp(x),0,3);
%function for which integration have to do
f=@(x) x.^2.*exp(x);
%upper and lower limit
a=0;b=3;

fprintf('\nFor the function having limit a=%f to b=%f, f(x)=',a,b)
disp(f)
fprintf('\tExact Integration value for the function is %f.\n',I_ext);


%Integration using Trapizoidal rule to get approximation correct upto two
%decimals

for n=1:1000
  
    I_trap=Trapizoidal_int(f,a,b,n);
    %checking wheather it correct upto two decimal or not
    if abs(I_trap-I_ext)<=10^-3
      
        fprintf('Integration value for the function using Trapizoidal rule is %f.\n',I_trap);
        break
      
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Matlab function for midpoint Method
function val=Midpoint_int(f,a,b,n)
    % f is the function for integration
    % a is the lower limit of integration
    % b is the upper limit of integration
    % n is the number of trapizoidal interval in [a,b]
    dx=(b-a)/n; %x interval
    val=0;
    %splits interval a to b into n+1 subintervals
    x=linspace(a,b,n+1);
    %loop for trapizoidal integration
        for i=1:n
            x_mid(i)=(x(i+1)+x(i))/2;
            val=val+double(f(x_mid(i)));
        end
    %result using midpoint integration method
    val=dx*val;
end

%%Matlab function for Trapizoidal Method
function val=Trapizoidal_int(f,a,b,n)
    % f is the function for integration
    % a is the lower limit of integration
    % b is the upper limit of integration
    % n is the number of trapizoidal interval in [a,b]
    dx=(b-a)/n; %x interval
    val=0;
    %splits interval a to b into n+1 subintervals
    xx=linspace(a,b,n+1);
    %loop for trapizoidal integration
        for i=2:n
            val=val+2*double(f(xx(i)));
        end
        %Final integration value val for limit a to b
        val=(dx/2)*(val+f(a)+f(b));
end
  
%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
T-Mobile 5:33 PM < Back MATH 1620 Pr… aビ Phase 2 In this phase, we will evaluate the integral nu...
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
  • 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,...

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