You are on page 1of 16

NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2

200332 5/1/2022

EPF3109 Numerical and Computer Methods


Assignment 8: Solving Nonlinear and Linear Equations

Question 1

Editor: assignment8q1.m
F1=@(x,y) y-0.5*(exp(x/2)+exp(-x/2));
F2=@(x,y) 9*x^2+25*y^2-225;
F1x=@(x) -(exp(x/2)-exp(-x/2))/4;
F2x=@(x) 18*x;
F2y=@(y) 50*y;
Jacob=@(x,y)-(exp(x/2)-exp(-x/2))/4*50*y-18*x;
xi=2.5;
yi=2;
Err=0.001;
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

for i=1:5
Jac=Jacob(xi,yi);
Delx=(-F1(xi,yi)*F2y(yi)+F2(xi,yi))/Jac;
Dely=(-F2(xi,yi)*F1x(xi)+F1(xi,yi)*F2x(xi))/Jac;
xipl=xi+Delx;
yipl=yi+Dely;
Errx=abs((xipl-xi)/xi);
Erry=abs((yipl-yi)/yi);
fprintf('i=%1.0f x=%-7.4f Error in x=%-7.4f Error in y=%-7.4f
\n',i,xipl,yipl,Errx,Erry)
if Errx<Erry
break
elseif Erry<Errx
break
else
xi=xipl;
yi=yipl;
endif
endfor

Command Window
i=1 x=3.1388 Error in x=2.4001 Error in y=0.2555
i=0
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

Question 2

a)
Editor: assignment8q2a.m
F=@(x)3+3*sin(x)-0.5*x^3;
fplot(F,[-5,5])
xlabel('x')
ylabel('f(x)')
x1=fzero(F,-1.5)
x2=fzero(F,2.2)

Command Window
>> assignment8q2a
x1 = 2.2112
x2 = 2.2112
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

b)
Editor: assignment8q2b.m
F=@(x)x^3-8*x^2+17*x+x^(1/2)-10;;
fplot(F,[0,5])
xlabel('x')
ylabel('f(x)')
x1=fzero(F,0.8)
x2=fzero(F,2.4)
x3=fzero(F,4.8)

Command Window
>> assignment8q2b
x1 = 0.8173
x2 = 2.4244
x3 = 4.7934
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

c)
Editor: assignment8q2c.m
F=@(x)x^2-5*x*sin(3*x)+3;
fplot(F,[0,5])
xlabel('x')
ylabel('f(x)')
x1=fzero(F,2.4)
x2=fzero(F,2.8)

Command Window
>> assignment7q2c
x1 = 2.3656
x2 = 2.8435
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

Question 3

Editor: assignment8q3.m
a=0.22;
b=0.08;
K1=1600;
K2=100000;
W=400;
L0=sqrt(a^2+b^2);
L=@(x) sqrt(a.^2+((b+x).^2));
Fs=@(x) (L(x)-L0).*K1+(L(x)-L0).^3.*K2;
xp=0:0.01:0.25;
Fp=2.*Fs(xp).*(b+xp)./L(xp);
plot(xp,Fp)
xlabel('x (m)','fontsize',15)
ylabel('W (N)','fontsize',15)
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

f=@(x) 2*Fs(x).*(b+x)./L(x)-W;
x=fzero(f,0.1)

Command Window
>> assignment8q3
x = 0.1729
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

Question 4

Editor: assignment8q4.m
A=input('Input matrix left: ');
B=input('Input matrix right: ');
[m,n]=size(A);
nb=n+1;
Sol=[A,B];
for k=1:n-1
for i=k+1:n
factor=Sol(i,k)/Sol(k,k);
Sol(i,k:nb)=Sol(i,k:nb)-factor*Sol(k,k:nb);
endfor
endfor
x=zeros(n,1);
x(n)=Sol(n,nb)/Sol(n,n);
for i=n-1:-1:1
x(i)=(Sol(i,nb)-Sol(i,i+1:n)*x(i+1:n))/Sol(i,i);
endfor
disp(x)

Command Window
>> assignment8q4

䜧᐀Input matrix left: [2 1 -1 4;-1 -2 1 2;2 4 2 1;-1 1 -1 -2]

Input matrix right: [19;-3;25;-5]


2
4
1
3
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

Question 5

Editor: assignment8q5.m
A=[9 -2 3 2;2 8 -2 3;-3 2 11 -4; -2 3 2 10];
B=[54.5;-14;12.5;-21];
maxerror=1e-5;
x=zeros(1,size(A,1));
error=Inf;
itr=0;
while all(error>maxerror)
x_old=x;
for i=1:size(A,1)
sum=0;
for j=1:i-1
sum=sum+A(i,j)*x(j);
endfor
x(i)=(1/A(i,i))*(B(i)-sum);
endfor
itr=itr+1;
error=abs(x_old-x);
end
fprintf('Method converge in %d iterations \n',itr)
disp('x=')
disp(x)

Command Window
>> assignment8q5
Method converge in 2 iterations
x=
6.0556 -3.2639 3.3813 -0.5860
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

Question 6

Editor: f_nonlinear.m
function[function_value]=f_nonlinear(i,x)
ca=50;
cb=20;
cc=5;
cd=10;
if 1==1
func=cc+x(1)+x(2)/(cb-x(1))*(ca-2*x(1)-x(2))^2;
end
if i==2
func=cc+x(1)+x(2)/ca-2*x(1)-x(2)*(cd-x(2));
end
function_value=func;

Jacobian.m
function[Jac]=Jacobian(i,j,x)
del=10^-5;
beta(:,1)=x(:,1);
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

beta(j,1)=beta(j,1)-del;
Jac=(f_nonlinear(i,x)-f_nonlinear(i,beta))/del;
end

NewtonRaphson.m
%main
x(1,1)=0.0004;
x(2,1)=0.037;
N=2;
Kmax=100;
for k=1:Kmax
fprintf('Iteration number %12i\n',k)
for i=1:N
for j=1:N
J(i,j)=Jacobian(i,j,x);
end
b(i,1)=f_nonlinear(i,x);
end

fprintf('The Jacobian at the current iteration: \n')


disp(J)
disp('')
xold=x;
z=linsolve(J,-b);
x=xold+z;

for i=1:N
Res(i)=f_nonlinear(i,x);
end

residual(k)=norm(Res);

%print to screen
fprintf('Residualnorm %12.8e \n',residual(k))
fprintf('current solution \n')
fprintf('%12.8f \n',x(:,1))
disp('')

%quit if converged
if residual(k)<=10^-5 break
end
%quit if diverged
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

if residual(k)>=-10e+15 break
end
end

%print solution to screen


fprintf(' x \n')
fprintf('%12.5e \n',x(:,1))
disp('')
fprintf('Residual: \n')
fprintf('%12.5e \n',transpose(residual(:)))

Command Window
>> NewtonRaphson
Iteration number 1
The Jacobian at the current iteration:
0.8612 124.6288
-1.0000 -9.9060

Residualnorm 2.42776680e+00
current solution
5.79314248
-0.08020496

x
5.79314e+00
-8.02050e-02

Residual:
2.42777e+00
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

Question 7

Editor: assignment8q7.m
A=[0.2 0.2 0.3 0.2 0.2;28 1 0 0 0.1;0 18 12 2.4 16;0 0 10 0 1;0 0 0 10 2;0 0 0
0 18];
b=[3.4;20.5;170;49;39.8;96.3];
x=A\b

Command Window
>> assignment8q7
x =
0.6634
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

1.3909
4.3650
2.9100
5.3500

Concentration of CH4 = 0.6634


Concentration of C2H4 = 1.3909
Concentration of C2H6 = 4.3650
Concentration of C3H6 = 2.9100
Concentration of C3H8 = 5.3500
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

Question 8

Editor: f1_assignment7q5.m
sucrose=10*15/100;
stabilizer=10*0.4/100;
eggyolk=10*1/100;
f1=10-sucrose-stabilizer-eggyolk

Command Window
>> f1_assignment8q8
f1 = 8.3600

First equation: x+y+z=8.36

Second equation:
(0.09)(100 − 3.5) (0.09)(100 − 35)
(0.97)𝑥 + 𝑦+ 𝑧 = (0.095)(10)
100 100
(0.97)𝑥 + (0.08685)𝑦 + (0.0585)𝑧 = 0.95

Third equation:
(0.035)𝑦 + (0.35)𝑧 = (0.18)(10) = 1.8
NUR NASHWA ALIA BINTI KHAIRUDIN EPF3109-2
200332 5/1/2022

1 1 1 𝑥 8.36
[0.97 0.08685 0.0585] [𝑦] = [0.95]
0 0.035 0.35 𝑧 1.8

Command Window
>> A=[1 1 1;0.97 0.08685 0.0585;0 0.035 0.35];
>> b=[8.36;0.95;1.8];
>> ans=A\b
ans =

0.4086
3.1206
4.8308

You might also like