Es84 Lab Fin
-
Upload
donna-ambalong -
Category
Documents
-
view
226 -
download
0
Transcript of Es84 Lab Fin
-
7/29/2019 Es84 Lab Fin
1/7
Meilyssa A. Mayormita ES84 lab
BSECE 3 BF78ATH67
CODES
Bisection Method
fprintf('\n~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~\n');
fprintf('This program will use Bisection to solve for the root of the');
fprintf('\nfunction f(x) found between xl and xu, given an error criterion.\n\n');
f = input('function : f(x) = ','s');
xl = input('lower limit of interval, xl : ');
xu = input('upper limit of interval, xu : ');
if xl>=xu
error('Input lower boundary must be less than input upper boundary.');end
errcrit = input('error criterion (in %) : ');
x=xl; fxl=eval(f);
x=xu; fxu=eval(f);
if fxl*fxu>0
error('No root exists in the given interval.');
end
if fxl==0
xr=xl; its=0; err=0;
elseif fxu==0xr=xu; its=0; err=0;
else
x=0.5*(xl+xu);
ft=eval(f);
if ft*fxl>0
xr=xl;
else
xr=xu;
end
its=0;
while (1)
xro=xr;
xr=0.5*(xl+xu);x=xl; fxl=eval(f);
x=xr; fxr=eval(f);
if (fxl*fxr>0)
xl=xr;
else
xu=xr;
end
err=100*abs((xr-xro)/xr);
if err
-
7/29/2019 Es84 Lab Fin
2/7
Newton-Raphson Method
fprintf('\n~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~\n');
fprintf('This program will use the Newton-Raphson Method to solve for');
fprintf('\nthe root of the function f(x), without requiring an interval,');
fprintf('\ngiven an initial value xi and an error criterion to be met.\n\n');
f = input('function : f(x) = ','s');xp = input('initial value, xi : ');
errcrit = input('error criterion (in %) : ');
its=0;
while (1)
x=xp; fxp=eval(f);
x=x+1e-11; fxq=eval(f);
dfxp=(fxq-fxp)/1e-11;
xn=xp-fxp/dfxp;
xo=xp;
xp=xn;
err=100*abs((xp-xo)/xp);
its=its+1;if err=xu
error('Input lower boundary must be less than input upper boundary.');
end
p = input('number of panels , p : ');
bet=0;
d=(xu-xl)/p;
i=0;
x=xl;
while i
-
7/29/2019 Es84 Lab Fin
3/7
Simpsons 1/3 Rule
fprintf('\n~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~\n');
fprintf('This program will use Simpson''s 1//3 Rule to solve for the');
fprintf('\nintegral of the function f(x) from xl to xu, given p panels.\n');
fprintf('Note that this can only be used when p is even.\n\n');
f = input('function : f(x) = ','s');xl = input('lower limit of integral, xl : ');
xu = input('upper limit of integral, xu : ');
if xl>=xu
error('Input lower boundary must be less than input upper boundary.');
end
p = input('number of panels , p : ');
if rem(p,2)~=0
error('Number of panels must be even.');
end
d=(xu-xl)/p;betodd=0;
i=1;
x=xl+d;
while i
-
7/29/2019 Es84 Lab Fin
4/7
Gauss-Seidel Method
clear;
fprintf('\n\n~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~\n');
fprintf('This program will solve for the solution vector [x] in [A][x]=[b],');
fprintf('\ngiven the error criterion for maximum error among all variables.\n\n');n = input('size of matrix A (nxn) : n = ');
if ~(n>0)
error('n must be a positive.');
end
for i=1:n
j=1;
while j
-
7/29/2019 Es84 Lab Fin
5/7
SAMPLE OUTPUT
Bisection Method Sample Output
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
This program will use Bisection to solve for the root of the
function f(x) found between xl and xu, given an error criterion.
function : f(x) = exp(x)/(1+exp(x))-3*cos(x)+(x.^3)/11
lower limit of interval, xl : 0
upper limit of interval, xu : 5
error criterion (in %) : 1e-11
root = 1.24742
percent error = 5.6961e-012%
number of iterations = 45
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Newton-Raphson Method Sample Output
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
This program will use the Newton-Raphson Method to solve for
the root of the function f(x), without requiring an interval,
given an initial value xi and an error criterion to be met.
function : f(x) = exp(x)/(1+exp(x))-3*cos(x)+(x.^3)/11initial value, xi : 0
error criterion (in %) : 1e-11
root = 1.24742
percent error = 4.45008e-013%
number of iterations = 10
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
-
7/29/2019 Es84 Lab Fin
6/7
Trapezoidal Rule Sample Output
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
This program will use the Trapezoidal Rule to solve for the
integral of the function f(x) from xl to xu, given p panels.
function : f(x) = exp(x)/(1+exp(x))
lower limit of integral, xl : 0
upper limit of integral, xu : 5
number of panels , p : 1
The integral of f(x) from 0 to 5 is
3.73327
with percent error of 13.4529%.
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
This program will use the Trapezoidal Rule to solve for the
integral of the function f(x) from xl to xu, given p panels.
function : f(x) = exp(x)/(1+exp(x))
lower limit of integral, xl : 0
upper limit of integral, xu : 5
number of panels , p : 9
The integral of f(x) from 0 to 5 is
4.30729
with percent error of 0.14551%.
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Simpsons 1/3 Rule Sample Output
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
This program will use Simpson's 1//3 Rule to solve for the
integral of the function f(x) from xl to xu, given p panels.
Note that this can only be used when p is even.
function : f(x) = exp(x)/(1+exp(x))
lower limit of integral, xl : 0
upper limit of integral, xu : 5number of panels , p : 2
The integral of f(x) from 0 to 5 is
4.3249
with percent error of 0.262594%.
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
This program will use Simpson's 1//3 Rule to solve for the
integral of the function f(x) from xl to xu, given p panels.
Note that this can only be used when p is even.
function : f(x) = exp(x)/(1+exp(x))
lower limit of integral, xl : 0
upper limit of integral, xu : 5
number of panels , p : 88
The integral of f(x) from 0 to 5 is
4.31357
with percent error of 1.76478e-007%.
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
-
7/29/2019 Es84 Lab Fin
7/7
Gauss-Seidel Method Sample Output
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
This program will solve for the solution vector [x] in [A][x]=[b],
given the error criterion for maximum error among all variables.
size of matrix A (nxn) : n = 3
a11 : 3
a12 : -0.1
a13 : -0.2
a21 : 0.1
a22 : 7
a23 : -0.3
a31 : 0.3
a32 : -0.2
a33 : 10
A =
3.0000 -0.1000 -0.2000
0.1000 7.0000 -0.3000
0.3000 -0.2000 10.0000
b1 : 7.85
b2 : -19.3
b3 : 71.4
b =
7.8500
-19.3000
71.4000
error criterion (in %) : 1e-11
x =
3.0000
-2.5000
7.0000
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~