issue of science/5d.mat
x:[512x1 double array]
x1:[512x1 double array]
issue of science/Code_1.m
%% problem No.1
% Clear Screen
clc
clear all
disp('Bisection Method');
% Define constants
a1 = 1.42;
a2 = -7.89;
a3 = 0.52;
a4 = 0.047;
% Define Function
f = @(x) x^3 - (a1-exp(a2*x))*x^2 + a3*x - a4;
% starting interval
low = 0;
high = 1;
% tol
tol = 1e-12;
% Evaluate both ends of the interval
y1 = feval(f, low);
y2 = feval(f, high);
i = 0;
% Display error and finish if signs are not different
if y1 * y2 > 0
disp('Have not found a change in sign. Will not continue...');
return
end
while (abs(high - low) >= tol)
i = i + 1;
% Find a new value to be tested as a root
m = (high + low)/2;
y3 = feval(f, m);
if y3 == 0
return
end
% Update the limits
if y1 * y3 > 0
low = m;
y1 = y3;
else
high = m;
end
end
% Show the last approximation considering the tolerance
w = feval(f, m);
fprintf('\n x = %f produces f(x) = %g \n %i iterations\n', m, y3, i-1);
fprintf(' Approximation with tolerance = %g \n\n\n', tol);
fprintf(' Zero Found | a | b | Iteration Used\n')
fprintf(' %0.4f | %d | %d | %d\n\n',m,0,1,i-1)
issue of science/Code_2_a.m
%% Problem No. 2 a
% clear screen
clc
clear all
%% Secant Method initilization
% Define constants
a_1 = 0.1;
a_2 = -3.2;
a_3 = -5;
a_4 = -1;
syms x;
% Define function
f=sin(a_1+(a_2 - exp(a_3*x))*x^2 + a_4*x^3);
% tolerance
epsilon = 1e-12;
%initial guess
x0 = 0;
x1 = 1;
% secant method
for i=1:100
f0=vpa(subs(f,x,x0));
f1=vpa(subs(f,x,x1));
y=x1-((x1-x0)/(f1-f0))*f1;
err=abs(y-x1);
if err
break
end
x0=x1;
x1=y;
end
y = y - rem(y,1e-12);
% Display roots
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n\n\n',i);
fprintf('Zero Found | Resudial | NOI\n')
fprintf(' %0.4f | %0.4e | %d\n\n',y,double(err),i)
issue of science/Code_2_b.m
%% Problem No. 2 b
% clear screen
clc
clear all
%% Newton's Method to find roots
% Define constants
a_1 = 0.1;
a_2 = -3.2;
a_3 = -5;
a_4 = -1;
syms x;
f=sin(a_1+(a_2 - exp(a_3*x))*x^2 + a_4*x^3); %Enter the Function here
g=diff(f); %The Derivative of the Function
n=11;
epsilon = 10^-(n+1)
x0 = -0.1;
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the value of function at x0
f0_der=vpa(subs(g,x,x0)); %Calculating the value of function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
if err
break
end
x0=y;
end
y = y - rem(y,10^-n); %Displaying upto required decimal places
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n',i);
issue of science/Code_3_b.m
%% problem 3. b
% clear screen
clc
clear all
%% Broyden's Method
x0=[1;1;1;1*10^5];
tol = 1e-8;
nmax = 200;
gam = 9791;
z=[-19 -7 2];
c=[3.721 64.55 21.273];
J =@(x) fun_QpJ(x,gam,c,z);
invJ =@(x) inv(fun_QpJ(x,gam,c,z));
F =@(x) fun_Qp(x,gam,c,z);
x1 = x0;
J1 = J(x0);
n=0;
delF=1;
while tol
x2 = x1 - inv(J1) * F(x1);
delx = x2-x1;
delF = F(x2)-F(x1);
J2 = J1 + ((delF - J1*delx)./(abs(x2).^2))*delx';
x1=x2;
J1=J2;
n=n+1;
end
fprintf(' Resudial | NOI | Q2 | P\n')
fprintf(' %0.4e | %d | %0.4f | %0.3e\n\n',abs(mean(delF)),n,x1(2),x1(4))
issue of science/Code_3_c.m
%% Problem No. 3 c
% clear screen
clc
clear all
%% Newtons's Method to solve non linear equation
% initial guess
x0=[1;1;1;1*10^5];
% tol
tol = 1e-8;
% maximum iteration
nmax = 200;
% remaining constant
gam = 9791;
z=[-19 -7 2];
c=[3.721 64.55 21.273];
% anomly. function
J =@(x) fun_QpJ(x,gam,c,z);
invJ =@(x) inv(fun_QpJ(x,gam,c,z));
F =@(x) fun_Qp(x,gam,c,z);
x1 = x0;
n=0;
delF=1;
while tol
x2 = x1 - inv(J(x1)) * F(x1);
x1=x2;
n=n+1;
end
fprintf(' Q1 | Q3 | NOI\n')
fprintf(' %0.4f | %0.4f | %d\n\n',x1(1),x1(3),n)
issue of science/Code_4_a.m
%% Problem No. 4 a
% clear screen
clc
clear all
%% using fuction fun_gravity_flow and fun_gravity_flow_J cmopute table
theta=ones(20,1);
g=32.16;
Del_y=0.19;
n=1:20;
v=sqrt(2*g.*n*Del_y);
v=v';
X=1.9;
% compute function and jacobian
J =fun_gravity_flow_J(theta, v, X, Del_y);
F =fun_gravity_flow(theta, v, X, Del_y);
% print Table
fprintf(' f11 | f20 | J(13,13) | J(13,14) | J(13,16) | J(20,16)\n')
fprintf(' %0.4f | %0.4f | %0.4f | %0.4f | %0.4f | %0.4f \n\n',F(11),F(20),J(13,13),J(13,14),J(13,16),J(20,16))
issue of science/Code_4_b.m
%% Problem No. 4 b
% clear screen
clc
clear all
%% Newtons's Method to solve non-linear equations
theta0=ones(20,1);
g=32.16;
Del_y=0.19;
n=1:20;
v=sqrt(2*g.*n*Del_y);
v=v';
X=1.9;
tol = 1e-12;
nmax = 200;
J =@(theta) fun_gravity_flow_J(theta, v, X, Del_y);
invJ =@(theta) inv(fun_gravity_flow_J(theta, v, X, Del_y));
F =@(theta) fun_gravity_flow(theta, v, X, Del_y);
x1 = theta0;
n=0;
delF=1;
while tol
x2 = x1 - inv(J(x1)) * F(x1);
x1=x2;
n=n+1;
end
% Print Results
fprintf(' theta3 | theta13 | theta18 | Resudial | NOI\n')
fprintf(' %0.4f | %0.4f | %0.4f | %0.3e | %d\n\n',x1(3),x1(13),x1(18),abs(mean(F(x1))),n)
issue of science/Code_4_c.m
%% Problem No. 4 c
clc
clear all
%% Broyden's Method
theta0=ones(20,1);
g=32.16;
Del_y=0.251;
n=1:20;
v=sqrt(2*g.*((n+1)./n).*Del_y);
v=v';
X=1.98;
tol = 1e-12;
nmax = 200;
J =@(theta) fun_gravity_flow_J(theta, v, X, Del_y);
invJ =@(theta) inv(fun_gravity_flow_J(theta, v, X, Del_y));
F =@(theta) fun_gravity_flow(theta, v, X, Del_y);
x1 = theta0;
J1 = J(theta0);
n=0;
delF=1;
while tol
x2 = x1 - inv(J1) * F(x1);
delx = x2-x1;
delF = F(x2)-F(x1);
J2 = J1 + ((delF - J1*delx)./(abs(x2).^2))*delx';
x1=x2;
J1=J2;
n=n+1;
end
% print Results
fprintf(' theta1 | theta11 | theta17 | Resudial | NOI\n')
fprintf(' %0.4f | %0.4f | %0.4f | %0.3e | %d\n\n',x1(1),x1(11),x1(17),abs(mean(F(x1))),n)
issue of science/Code_5_a.m
%% problem 5. a
clc
clear all
N = 5;
h=1/(1+N);
x(1)=h;
for i=2:N
x(i)=x(i-1)+h;
end
for i=1:N
g(i) = exp(-2*x(i))*(sin(x(i))*(exp(2*x(i))+(exp(1)-exp(x(i)))^2*sin(x(i)))-2*exp(x(i)+1)*cos(x(i)));
end
u=[1 1 1 1 1]';
g=g';
f=fu(u,g,h);
J=fu_J(u,g,h);
% print result
fprintf(' f1 | f3 | J(2,2) | J(2,3) | J(2,4) | J(3,3) | J(3,2) | J(3,1)\n')
fprintf(' %0.4f | %0.4f | %0.4f | %0.4f | %0.4f | %0.4f | %0.4f | %0.4f\n\n',f(1),f(3),J(2,2),J(2,3),J(2,4),J(3,3),J(3,2),J(3,1))
issue of science/Code_5_b.m
%% Problem 5 b
clc
clear all
%% Newtons's Method
N = 512;
h=1/(1+N);
x(1)=h;
for i=2:N
x(i)=x(i-1)+h;
end
for i=1:N
g(i) =...