Upcoming SlideShare
×

# Assignment 3

166
-1

Published on

mathematical theories assignmemt

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
166
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Assignment 3

1. 1. Assignment (3)Q1. Solve the integral using paper and pen or calculator, andthen compare the answer to the computational method using the Romberg method.Solution= [x6 /6 + x5 /5 + x4/ 4 + ex]={ [1/6 (4)6 + 1/5 (4)5 + 1/4(4)4 + e4] –[1/6 (0.5)6 + 1/5 (0.5)5 + 1/4(0.5)4 + e5] }= (1006.06) –(1.752865) = 1004.3071function [q,ea,iter]=romberg(func,a,b,es,maxit,varargin)% if nargin<3,error(at least 3arguments required),endif nargin<4||isempty(es),es=0.000001;endif nargin<5||isempty(maxit),maxit=50;endn=1;r(1,1) = trap(func,a,b,n,varargin{:});iter=0;while iter<maxit iter=iter+1; n=2^iter; I(iter+1,1)=trap(func,a,b,n,varargin{:}); for k=2:iter+1 j=2+iter-k; I(j,k)=(4^(k-1)*I(j+1,k-1)-I(j,k-1))/(4^(k-1)-1); end ea=abs((I(1,iter+1)-I(2,iter))/I(1,iter+1))*100; if ea<=es, break; endendq=I(1,iter+1);function I=trap(func,a,b,n,varargin)if(nargin<3) error(at least 3 input arguments required);
2. 2. endif ~(b>a),error(upper bound must be greater than lower),endif nargin<4||isempty(n),n=100;end x=a; h=(b-a)/n; s=func(a,varargin{:}); for i=1:n-1 x=x+h; s=s+2*func(x,varargin{:}); end s=s+func(b,varargin{:}); I=(b-a)*s/(2*n);>> f=@(x) x^5+x^4+x^3+exp(x);>> romberg(f,0.5,4)ans = 1.0044e+003
3. 3. Q2. Calculate the integral dt. The exact answer to this integral is15.41260804810169. Solve the integral using the Romberg methodSolutionfunction [q,ea,iter]=romberg(func,a,b,es,maxit,varargin)% if nargin<3,error(at least 3arguments required),endif nargin<4||isempty(es),es=0.000001;endif nargin<5||isempty(maxit),maxit=50;endn=1;r(1,1) = trap(func,a,b,n,varargin{:});iter=0;while iter<maxit iter=iter+1; n=2^iter; I(iter+1,1)=trap(func,a,b,n,varargin{:}); for k=2:iter+1 j=2+iter-k; I(j,k)=(4^(k-1)*I(j+1,k-1)-I(j,k-1))/(4^(k-1)-1); end ea=abs((I(1,iter+1)-I(2,iter))/I(1,iter+1))*100; if ea<=es, break; endendq=I(1,iter+1);function I=trap(func,a,b,n,varargin)if(nargin<3) error(at least 3 input arguments required);endif ~(b>a),error(upper bound must be greater than lower),endif nargin<4||isempty(n),n=100;end x=a; h=(b-a)/n; s=func(a,varargin{:}); for i=1:n-1 x=x+h; s=s+2*func(x,varargin{:}); end s=s+func(b,varargin{:}); I=(b-a)*s/(2*n);
4. 4. >> f=@(t) (10*exp(-t)*sin(2*pi*t))^2>> romberg (f,0,0.5)ans = 15.4126