Answer To: Computer Project 3: Numerical Integration Note: Make sure to use the matlab ’format long e’ to print...
Rahul answered on May 01 2021
Math_280420/c_2.m
% Matlab Code to change string format function to function handle
function fun = c_2(f)
str = strcat('@','(x)',f)
fun = str2func(str)
end
Math_280420/d_2.m
% Matlab Code to change string format function to function handle
function fun = c_2(f)
str = strcat('@','(x)',f)
fun = str2func(str)
end
Math_280420/f2d.m
% Matlab Code to change 2 d string format function to function handle
function fun = f2d(f)
str = strcat('@','(x,y)',f)
fun = str2func(str)
end
Math_280420/gauss.m
function [xi,w] = gauss(ng)
% for ng=1,2,3,4,5,6,7 and 10.
% call as
% >[xi,w]=gauss(5)
% for 5 point-quadrature
%
switch ng
case 1
xi = [0.0];
w = [2.0];
case 2
xi = [-1.0/sqrt(3.0),1.0/sqrt(3.0)];
w = [1.0,1.0];
case 3
xi = [-sqrt(3.0/5.0), 0.0, sqrt(3.0/5.0)];
w = [ 5.0/9.0, 8.0/9.0,5.0/9.0];
case 4
xi = [-0.861136311594053,-0.339981043584856];
xi = [xi,-xi];
w = [ 0.347854845137454, 0.652145154862546];
w = [w,w];
case 5
xi = [-0.906179845938664,-0.538469310105683];
xi = [xi,0,-xi];
w = [0.236926885056189,0.478628670499366];
w = [w,0.568888888888889,w];
case 6
xi = [-0.932469514203152, -0.661209386466265,- 0.238619186083197];
xi = [xi,-xi];
w = [0.171324492379170,0.360761573048139,0.467913934572691];
w = [w,w];
case 7
xi = [-0.949107912342759,-0.741531185599394,-0.405845151377397];
xi = [ xi,0.0,-xi];
w = [0.129484966168870,0.279705391489277, 0.381830050505119];
w = [w,0.417959183673469,w];
otherwise
xi = [ -0.973906528517172,-0.865063366688985,-0.679409568299024,-0.433395394129247,-0.148874338981631];
xi = [ xi,-xi];
w = [ 0.066671344308688,0.149451349150581,0.219086362515982,0.269266719309996,0.295524224714753];
w = [ w ,w ];
end;
Math_280420/myf.m
% Matlab function to change string to function in this independent variable
% is x
% Input f in string format
% Ouput function_handle
function fun = myf(f)
str = strcat('@','(x)',f)
fun = str2func(str)
end
Math_280420/myf_1.m
% Matlab function to change string to function in this independent variable
% is x
% Input f in string format
% Ouput function_handle
function fun = myf_1(f)
str = strcat('@','(t)',f)
fun = str2func(str)
end
Math_280420/mygauss.m
% Matlab code to find the value of integration by gauss method
%% Input:
% f : function in the string format
% a = Lower Limit
% b = Upper limit
% ng = Number of gauss points
%%Function Run
function I = mygauss(f,a,b,ng)
[xi,w] = gauss(ng); % Find the xi and weight from the gauss function
f = myf(f);% Change string function to function_handle
I = 0; % Initialise the integration with zero value
% Shifting of the x = (a+b)/2 + (b-a)*xi/2 and dx = (b-a)/2*dxi
for i = 1:ng % Loop to find the integration value
I = I + w(i)*(b-a)/2*feval(f,((b-a)*xi(i)/2)+(b+a)/2);
end
%% output: I is the value of the integration
disp(['Value of the integration is = ',num2str(I)]);
end
Math_280420/mygauss_1.m
% Matlab code to find the value of integration by gauss method
%% Input:
% f : function in the string format
% a = Lower Limit
% b = Upper limit
% ng = Number of gauss points
%%Function Run
function I = mygauss_1(f,a,b,ng)
[xi,w] = gauss(ng); % Find the xi and weight from the gauss function
f = myf_1(f);% Change string function to function_handle
I = 0; % Initialise the integration with zero value
% Shifting of the x = (a+b)/2 + (b-a)*xi/2 and dx = (b-a)/2*dxi
for i = 1:ng % Loop to find the integration value
I = I + w(i)*(b-a)/2*feval(f,((b-a)*xi(i)/2)+(b+a)/2);
end
%% output: I is the value of the integration
disp(['Value of the integration is = ',num2str(I)]);
end
Math_280420/mygauss2D.asv
% Matlab Code to find double integration value by using gauss quadrature
function I = mygauss(f,a,b,ng)
%ng = 2;
[xi,w1] = gauss(ng);% find the weight by using ng point
[yi,w2] = gauss(ng);
%f = '(x^2 + y^2)*cos(x)';
f = f2d(f);% change string format function to function handle
% a = 0;
% b = 2.5;
% c = @(x)cos(x);
% d = @(x)2*exp(x);
% d_1 = @(x)2*exp(x);
% c_1 = @(x)-1*sin(x);
Iy = 0;% Intial valye of integration is zero
for i = 1:ng % loop to find the value of the inegration
%shifted quadrature are obtained using the mapping from
Iy = Iy + w1(i)*w2(i)*((d(a + ((b-a)*(1+xi(i))/2))-c(a + ((b-a)*(1+xi(i))/2)))/2)*((b-a)/2)*f(a + ((b-a)*(1+xi(i))/2),c(xi(i)) + ...
((d(a + ((b-a)*(1+xi(i))/2))-c(d(a + ((b-a)*(1+xi(i))/2))))*(1 + yi(i))/2)) %+ (1+yi(i))*w1(i)*w1(i)*((d_1(a + ((b-a)*(1+xi(i))/2))-c_1(a + ((b-a)*(1+xi(i))/2)))/2)*((b-a)/2)*((b-a)/2)*f(a + ((b-a)*(1+xi(i))/2),c(xi(i)) + ...
%((d(a + ((b-a)*(1+xi(i))/2))-c(d(a + ((b-a)*(1+xi(i))/2))))*(1 + yi(i))/2))
end
disp(['Value of the integration is = ',num2str(Iy)]);
end
Math_280420/mygauss2D.m
% Matlab Code to find double integration value by using gauss quadrature
function Iy = mygauss2D(f,a,b,c,d,ng)
%ng = 2;
[xi,w1] = gauss(ng);% find the weight by using ng point
[yi,w2] = gauss(ng);
%f = '(x^2 + y^2)*cos(x)';
f = f2d(f);% change string format function to function handle
% a = 0;
% b = 2.5;
% c = @(x)cos(x);
% d = @(x)2*exp(x);
% d_1 = @(x)2*exp(x);
% c_1 = @(x)-1*sin(x);
Iy = 0;% Intial valye of integration is zero
for i = 1:ng % loop to find the value of the inegration
%shifted quadrature are obtained using the mapping from [-1,1] to
%[a,b]x [c,d]
% x = a + (b-a)*(1 + xi)/2
% y = c(xi) + (d(a + (b-a)*(1 + xi)/2) - c(a + (b-a)*(1 + xi)/2)*(1 + yi)/2
Iy = Iy + w1(i)*w2(i)*((d(a + ((b-a)*(1+xi(i))/2))-c(a + ((b-a)*(1+xi(i))/2)))/2)*((b-a)/2)*f(a + ((b-a)*(1+xi(i))/2),c(xi(i)) + ...
((d(a + ((b-a)*(1+xi(i))/2))-c(d(a + ((b-a)*(1+xi(i))/2))))*(1 + yi(i))/2)) %+ (1+yi(i))*w1(i)*w1(i)*((d_1(a + ((b-a)*(1+xi(i))/2))-c_1(a + ((b-a)*(1+xi(i))/2)))/2)*((b-a)/2)*((b-a)/2)*f(a + ((b-a)*(1+xi(i))/2),c(xi(i)) + ...
%((d(a + ((b-a)*(1+xi(i))/2))-c(d(a + ((b-a)*(1+xi(i))/2))))*(1 + yi(i))/2))
end
disp(['Value of the integration is = ',num2str(Iy)]);
end
Math_280420/mygauss2D_m.asv
% Matlab Code to find double integration value by using gauss quadrature
function Iy = mygauss2D_m(f,a,b,c_1,d,ng)
%ng = 2;
[xi,w1] = gauss(ng);% find the weight by using ng point
[yi,w2] = gauss(ng);
%f = '(x^2 + y^2)*cos(x)';
f = f2d(f);% change string format function to function handle
c = c(c);
d = d(d);
% a = 0;
% b = 2.5;
% c = @(x)cos(x);
% d = @(x)2*exp(x);
% d_1 = @(x)2*exp(x);
% c_1 = @(x)-1*sin(x);
Iy = 0;% Intial valye of integration is zero
for i = 1:ng % loop to find the value of the inegration
%shifted quadrature are obtained using the mapping from [-1,1] to
%[a,b]x [c,d]
% x = a + (b-a)*(1 + xi)/2
% y = c(xi) + (d(a + (b-a)*(1 + xi)/2) - c(a + (b-a)*(1 + xi)/2)*(1 + yi)/2
Iy = Iy + w1(i)*w2(i)*((d(a + ((b-a)*(1+xi(i))/2))-c(a + ((b-a)*(1+xi(i))/2)))/2)*((b-a)/2)*f(a + ((b-a)*(1+xi(i))/2),c(xi(i)) + ...
((d(a + ((b-a)*(1+xi(i))/2))-c(d(a + ((b-a)*(1+xi(i))/2))))*(1 + yi(i))/2)) %+ (1+yi(i))*w1(i)*w1(i)*((d_1(a + ((b-a)*(1+xi(i))/2))-c_1(a + ((b-a)*(1+xi(i))/2)))/2)*((b-a)/2)*((b-a)/2)*f(a + ((b-a)*(1+xi(i))/2),c(xi(i)) + ...
%((d(a + ((b-a)*(1+xi(i))/2))-c(d(a + ((b-a)*(1+xi(i))/2))))*(1 + yi(i))/2))
end
disp(['Value of the integration is = ',num2str(Iy)]);
end
Math_280420/mygauss2D_m.m
% Matlab Code to find double integration value by using gauss quadrature
function Iy = mygauss2D_m(f,a,b,c_1,d_1,ng)
%ng = 2;
[xi,w1] = gauss(ng);% find the weight by using ng point
[yi,w2] = gauss(ng);
%f = '(x^2 + y^2)*cos(x)';
f = f2d(f);% change string format function to function handle
c = c_2(c_1);
d = d_2(d_1);
% a = 0;
% b = 2.5;
% c = @(x)cos(x);
% d = @(x)2*exp(x);
% d_1 = @(x)2*exp(x);
% c_1 = @(x)-1*sin(x);
Iy = 0;% Intial valye of integration is zero
for i = 1:ng % loop to find the value of the inegration
%shifted quadrature are obtained using the mapping from [-1,1] to
%[a,b]x [c,d]
% x = a + (b-a)*(1 + xi)/2
% y = c(xi) + (d(a + (b-a)*(1 + xi)/2) - c(a + (b-a)*(1 + xi)/2)*(1 + yi)/2
Iy = Iy + w1(i)*w2(i)*((d(a + ((b-a)*(1+xi(i))/2))-c(a + ((b-a)*(1+xi(i))/2)))/2)*((b-a)/2)*f(a + ((b-a)*(1+xi(i))/2),c(xi(i)) + ...
((d(a + ((b-a)*(1+xi(i))/2))-c(d(a + ((b-a)*(1+xi(i))/2))))*(1 + yi(i))/2)) %+ (1+yi(i))*w1(i)*w1(i)*((d_1(a + ((b-a)*(1+xi(i))/2))-c_1(a + ((b-a)*(1+xi(i))/2)))/2)*((b-a)/2)*((b-a)/2)*f(a + ((b-a)*(1+xi(i))/2),c(xi(i)) + ...
%((d(a + ((b-a)*(1+xi(i))/2))-c(d(a + ((b-a)*(1+xi(i))/2))))*(1 + yi(i))/2))
end
disp(['Value of the integration is = ',num2str(Iy)]);
end
Math_280420/problem_2_4.m
format long e
% Matlab Code to find the length of the curve for given function
% Input is m and in this finding the length of the for different value of
% m
m = [2 4 8 16 32 64];
s = zeros(length(m),1); % length of the curve for corresponding to particular value of m
for k = 1:length(m) % for loop to take different m from the array
a = 0; % lower lomit
b = 2; % Upper Limit
% dividing the domain of the integration to number of intervals and m is
% number of intervel in domain range is divided
% for eg if 0 to 2 is domain and m is 2. It means number of interval is
% 2. So first find the integration between...