Вы находитесь на странице: 1из 18

Jawaban praktikum 2.

docx

Average temperature M-Files code

function averagetemp
t1=input('Day 1=');
t2=input('Day 2=');
c=input('Input city code using:',’s’);
w=2*pi/365;
tpeak=205;
switch c
case 'FL'
Tmean=22.1;
Tpeak=28.3;
case 'AZ';
Tmean=23.1;
Tpeak=33.6;
case 'ND';
Tmean=5.2;
Tpeak=22.1;
case 'WA';
Tmean=10.6;
Tpeak=17.6;
case 'MA';
Tmean=10.7;
Tpeak=22.9;
otherwise
error('City code is invalid')
end
T1=Tmean+(Tpeak-Tmean)*cos(w*(t1-tpeak));
T2=Tmean+(Tpeak-Tmean)*cos(w*(t2-tpeak));
fprintf('Day 1 average temperature= %1.2f \n',T1);
fprintf('Day 2 average temperature= %1.2f \n',T2);
end
function tank
%tank is a script used to solve problem 3.1 Chapra applied numerical
%method
%R= tank radius in meter
%d= water level in meter
%- if the water level value is below radius, only conical volume is
%accounted.
%- if the water level value is above radius, both conical and
cylindrical
%volume is accounted.
%- if the water level value is higher than 3 times the radius, an
error
%message will be displayed.
R= input('Tank radius (m):');
d= input('Water level (m):');
if d>3*R;
error('ERROR! Overtop. Try reducing water level!');
elseif d<=R;
v=1/3*pi*R^2*d;
else if d>R;
v=(1/3*pi*R^2*R)+(pi*R^2*(d-R));
end
end
fprintf('Water volume is %1.2f \n',v)
function [sinx, ea]= Sinfunctionapprox(x,n) %untuk membuat command
Sinfunctionapprox(x,n)
iter=1; %nilai iterasi awal
sinx=x; %nilai aproksimasi sin x awal
while iter<=n %Memberi syarat untuk terus melakukan loop selama nilai iter<=n
if iter==1 %jika iter masih 1
fprintf('Iteration level is %1.0f \n',iter)
fprintf('sin %1.2f approximation is %1.4f \n',x,sinx)
iter=iter+1;
z=mod(iter,2);
elseif z==0 %jika iter diatas 1 dan genap, karena deret mclaurin sin adalah negatif
pada iterasi genap
%even
sinxold=sinx;
sinx=sinx-(x^(iter+(iter-1))/(factorial((iter+(iter-1)))));
ea=abs((sinx-sinxold)/sinx*100);
fprintf('\nIteration level is %1.0f \n',iter)
fprintf('sin %1.2f approximation is %1.4f \n', x, sinx)
fprintf('relative error is %1.2f%%\n',ea)
iter=iter+1;
z=mod(iter,2);
elseif z==1 %jika iter diatas 1 dan ganjil, karena deret maclaurin sin adalah
positif pada iterasi ganjil
%odd
sinxold=sinx;
sinx=sinx+(x^(iter+(iter-1))/(factorial((iter+(iter-1)))));
ea=abs((sinx-sinxold)/sinx*100);
fprintf('\nIteration level is %1.0f \n',iter)
fprintf('sin %1.2f approximation is %1.4f \n', x, sinx)
fprintf('relative error is %1.2f%%\n',ea)
iter=iter+1;
z=mod(iter,2);
end
end
end
Jawaban praktikum 4.docx

>> f1=@(x) -2+(6*x)-(4*(x^2))+(0.5*(x^3))

f1 =

@(x)-2+(6*x)-(4*(x^2))+(0.5*(x^3))

>> f1

f1 =

@(x)-2+(6*x)-(4*(x^2))+(0.5*(x^3))

>> f2=@(x) 6-(8*x)+(1.5*(x^2))

f2 =

@(x)6-(8*x)+(1.5*(x^2))
a. >> newtraph(f1,f2,4.2)

ans =

0.4746

b. >> newtraph(f1,f2,4.43)

ans =

0.4746

a.
>> f1=@(x) (8*sin(x)*(exp(-x))-1)

f1 =

@(x)(8*sin(x)*(exp(-x))-1)

>> f2=@(x) (8.*sin(x).*(exp(-x))-1)


f2 =

@(x)(8.*sin(x).*(exp(-x))-1)
>> y=-1:0.01:1

y=

Columns 1 through 8 Columns 153 through 160

-1.0000 -0.9900 -0.9800 -0.9700 -0.9600 -0.9500 - 0.5200 0.5300 0.5400 0.5500 0.5600 0.5700
0.9400 -0.9300 0.5800 0.5900

Columns 9 through 16 Columns 161 through 168

-0.9200 -0.9100 -0.9000 -0.8900 -0.8800 -0.8700 - 0.6000 0.6100 0.6200 0.6300 0.6400 0.6500
0.8600 -0.8500 0.6600 0.6700

Columns 17 through 24 Columns 169 through 176

-0.8400 -0.8300 -0.8200 -0.8100 -0.8000 -0.7900 - 0.6800 0.6900 0.7000 0.7100 0.7200 0.7300
0.7800 -0.7700 0.7400 0.7500

Columns 25 through 32 Columns 177 through 184

-0.7600 -0.7500 -0.7400 -0.7300 -0.7200 -0.7100 - 0.7600 0.7700 0.7800 0.7900 0.8000 0.8100
0.7000 -0.6900 0.8200 0.8300

Columns 33 through 40 Columns 185 through 192

-0.6800 -0.6700 -0.6600 -0.6500 -0.6400 -0.6300 - 0.8400 0.8500 0.8600 0.8700 0.8800 0.8900
0.6200 -0.6100 0.9000 0.9100

Columns 41 through 48 Columns 193 through 200

-0.6000 -0.5900 -0.5800 -0.5700 -0.5600 -0.5500 - 0.9200 0.9300 0.9400 0.9500 0.9600 0.9700
0.5400 -0.5300 0.9800 0.9900

Columns 49 through 56 Column 201

-0.5200 -0.5100 -0.5000 -0.4900 -0.4800 -0.4700 - 1.0000


0.4600 -0.4500

Columns 57 through 64

-0.4400 -0.4300 -0.4200 -0.4100 -0.4000 -0.3900 -


0.3800 -0.3700

Columns 65 through 72

-0.3600 -0.3500 -0.3400 -0.3300 -0.3200 -0.3100 -


0.3000 -0.2900

Columns 73 through 80

-0.2800 -0.2700 -0.2600 -0.2500 -0.2400 -0.2300 -


0.2200 -0.2100

Columns 81 through 88

-0.2000 -0.1900 -0.1800 -0.1700 -0.1600 -0.1500 -


0.1400 -0.1300

Columns 89 through 96

-0.1200 -0.1100 -0.1000 -0.0900 -0.0800 -0.0700 -


0.0600 -0.0500

Columns 97 through 104

-0.0400 -0.0300 -0.0200 -0.0100 0 0.0100 0.0200


0.0300

Columns 105 through 112

0.0400 0.0500 0.0600 0.0700 0.0800 0.0900


0.1000 0.1100

Columns 113 through 120

0.1200 0.1300 0.1400 0.1500 0.1600 0.1700


0.1800 0.1900

Columns 121 through 128

0.2000 0.2100 0.2200 0.2300 0.2400 0.2500


0.2600 0.2700

Columns 129 through 136

0.2800 0.2900 0.3000 0.3100 0.3200 0.3300


0.3400 0.3500

Columns 137 through 144

0.3600 0.3700 0.3800 0.3900 0.4000 0.4100


0.4200 0.4300

Columns 145 through 152

0.4400 0.4500 0.4600 0.4700 0.4800 0.4900


0.5000 0.5100
>> d=f2(y);
>> plot(y,d),grid

b.
>> f3=@(x) (8*exp(-x)*cos(x))- (8*exp(-x)*sin(x))

f3 =

@(x)(8*exp(-x)*cos(x))-(8*exp(-x)*sin(x))

>> newtraph(f1,f3,0.3,[],3)

ans =

0.1450

c.
function [root,ea,iter]=newtraph(func,xr,xr2,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - func(xr)*(xr2-xr)/(func(xr2)-func(xr));
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;

>> newtraph(f1,f3,0.3,[],3)

ans =

0.1450

>> newtraph(f1,0.4,0.5)

ans =

0.1450

d.
function [root,ea,iter]=newtraph(func,xr,xr3,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - xr3*func(xr)/(func(xr+xr3)-func(xr));
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;

>> newtraph(f1,0.3,0.01)

ans =
0.1450

function [root,ea,iter]=newtraph(func,xr,xr2,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - func(xr)*(xr2-xr)/(func(xr2)-func(xr));
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;

>> f1=@(x) 5-(5*x)-(exp(0.5*x))

f1 =

@(x)5-(5*x)-(exp(0.5*x))

>> newtraph(f1,2,0)

ans =

0.7142
Ini yang terbaik karena tidak perlu diturunkan. Tinggal masukkan angka Xi dan Xi-1

a.
function [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;
>> f1=@(x) (x^3)-(2*(x^2))-(4*x)+8

f1 =

@(x)(x^3)-(2*(x^2))-(4*x)+8

>> f2=@(x) (3*(x^2))-(4*x)-4

f2 =

@(x)(3*(x^2))-(4*x)-4

>> newtraph(f1,f2,1.2)

ans =

2.0000

b.
function [root,ea,iter]=newtraph(func,dfunc,xr,m,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<5|isempty(es),es=0.0001;end
if nargin<6|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - m*(func(xr))/dfunc(xr);
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;
>> newtraph(f1,f2,1.2,2)

ans =
2.0000
c.
function [root,ea,iter]=newtraph(func,dfunc,xr,dfunc2,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<5|isempty(es),es=0.0001;end
if nargin<6|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - (func(xr)*dfunc(xr))/(((dfunc(xr))^2)-(func(xr)*dfunc2(xr)));
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;
>> newtraph(f1,f2,1.2,f3)
ans =

2.0000
Jawaban praktikum 5.docx
1. Tentukan penyelesaian sistem persamaan linier berikut ini

a. Menggunakan MATLAB command “\”


>> A=[1 2 -1;5 2 2;-3 5 -1]

A=

1 2 -1
5 2 2
-3 5 -1

>> b=[2;9;1]

b=

2
9
1

>> A\b

ans =

1
1
1

b. Menggunakan m-files nomer 1


function x = GaussPivot(A,b)
% GaussPivot: Gauss elimination pivoting
% x = GaussPivot(A,b): Gauss elimination with pivoting.
% input:
% A = coefficient matrix
% b = right hand side vector
% output:
% x = solution vector
[m,n]=size(A);
if m~=n, error('Matrix A must be square'); end
nb=n+1;
Aug=[A b];
% forward elimination
for k = 1:n-1
% partial pivoting
[big,i]=max(abs(Aug(k:n,k)));
ipr=i+k-1;
if ipr~=k
Aug([k,ipr],:)=Aug([ipr,k],:);
end
for i = k+1:n
factor=Aug(i,k)/Aug(k,k);
Aug(i,k:nb)=Aug(i,k:nb)-factor*Aug(k,k:nb);
end
end
% back substitution
x=zeros(n,1);
x(n)=Aug(n,nb)/Aug(n,n);
for i = n-1:-1:1
x(i)=(Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
end

>> Gauss(A,b)

ans =

1.0000
1.0000
1.0000

c. Excel Eliminasi Gauss dengan pivoting


Jawaban praktikum 5 excel.xlsx
Jawaban praktikum 6.xlsx

Gauss

Soal sama dengan LU

Вам также может понравиться