Академический Документы
Профессиональный Документы
Культура Документы
docx
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
f1 =
@(x)-2+(6*x)-(4*(x^2))+(0.5*(x^3))
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)
@(x)(8.*sin(x).*(exp(-x))-1)
>> y=-1:0.01:1
y=
-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
-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
-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
-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
-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
-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 57 through 64
Columns 65 through 72
Columns 73 through 80
Columns 81 through 88
Columns 89 through 96
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))
>> 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
>> 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=
1 2 -1
5 2 2
-3 5 -1
>> b=[2;9;1]
b=
2
9
1
>> A\b
ans =
1
1
1
>> Gauss(A,b)
ans =
1.0000
1.0000
1.0000
Gauss