Bendal, Angelo C. Bsme-2A Computer Programing

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

BENDAL, ANGELO C.

BSME-2A
COMPUTER PROGRAMING

1.1.2
b) ans =
a)
2
>> c = a - b
>> a = zeros(6,1)
3
a= c=
5
0 4 0 -4

0 c) 1.3.2

( 512π −0.255)
0 >> c = a .* b 1
a) θ0 = t = 0:0.1:9
0
c=
0
5 9 5 >> t = 0 : 0.1 : 9;

>> g = 9.8;
b) >> v0 = 50.75;
d)
>> b = 325 : 20 : 405 >> theta0 = 5*pi/12 - 0.255;
>> c = a ./ b
b= >> y0 = 0;
c=
325 345 365 385 405
>> x0 = 0;
5.0000 1.0000 0.2000
>> y = y0 - 0.5 * g * t.^2 + v0*sin(theta0).*t;
c) >> x = x0 + v0*cos(theta0).*t;
e)
>> a = sum(b) >> figure;
>> c = a .^2
a= >> plot(x,y);
c=
1825
>> title('y(t) vs. x(t)');
25 9 1
>> xlabel('Horizontal Distance (m)');
1.2.2 >> ylabel('Altitude (m)');
f)
a) >> grid on;
>> d = [1 2 3; 2 3 4; 4 5 6;]; d(1,:), d(:,2)
>> a = [5, 3, 1]; b = [1, 3, 5]; c = a + b
ans =
c=
1 2 3
6 6 6
b)
>> x = -pi/2 : pi/30 : 2*pi;

>> plot(x,atan(x));

>> title('y = atan(x) and y = acot(x)');

>> xlabel('x');

>> ylabel('y');

>> grid on;

>> hold on;

>> plot(x,acot(x),'r');

>> legend('y = atan(x)','y = acot(x)');



b) θ =
2
0
12 (
−0.425 ) t = 0:0.1:8

>> t = 0 : 0.1 : 8;

>> g = 9.8;
1.4.2

>> v0 = 50.75; a)
>> theta0 = 5*pi/12 - 0.425; >> x = -pi/2 : pi/30 : 2*pi;

>> y0 = 0; >> plot(x,atan(x),x,acot(x));

>> x0 = 0; >> title('y = atan(x) and y = acot(x)');

>> y = y0 - 0.5 * g * t.^2 + v0*sin(theta0).*t; >> xlabel('x');

>> x = x0 + v0*cos(theta0).*t; >> ylabel('y');

>> figure; >> legend('y = atan(x)','y = acot(x)');


c)
>> plot(x,y); >> grid on;
>> ezplot('(2/3)*sin(9*pi*x)',[0,2*pi]);
>> title('y(t) vs. x(t)');
>> title('High Frequency Cosine Function');
>> xlabel('Horizontal Distance (m)');
>> xlabel('x');
>> ylabel('Altitude (m)');
>> ylabel('y');
>> grid on;
>> grid on;
>> legend('O(n ln n)','O(sqrt(n))','O(ln n)') % Iterate 12 terms for our approximation.

exp_Approx12 = 0;

for j=0:12

exp_Approx12 = exp_Approx12 + x^(j)/factorial(j);

end

exp_Error12 = abs(exp_Approx12 - exp(x));

% Iterate 15 terms for our approximation.

exp_Approx15 = 0;

for j=0:15

exp_Approx15 = exp_Approx15 + x^(j)/factorial(j);end

exp_Error15 = abs(exp_Approx15 - exp(x));

fprintf('\nError with 12 terms:\n')


1.5.2
fprintf ('--------------------------\n')
Algorithm #1: O(n ln n)
fprintf ( 'exp(2): %g\n',exp_Error12 )

Algorithm #2: O(sqrt(n)) fprintf ('\nError with 15 terms: \n')

Algorithm #3: O(ln n) fprintf ('--------------------------\n')

fprintf ( 'exp(2): %g\n',exp_Error15)


>> n=1:0.01:500;

>> plot(n,n.*log(n),n,sqrt(n),n,log(n))
Error with 12 terms:
>> title('Big-O characteristics of Algorithms: Linear Plot')
--------------------------
>> ylabel('Estimate of Running Time')
exp(2): 1.5321e-06
>> xlabel('n (number of elements)')

>> legend('O(n ln n)','O(sqrt(n))','O(ln n)')


Error with 15 terms:
>> grid on; 2.1.2
--------------------------
2 3 n
x x x x exp(2): 3.54651e-09
e =1+ x + + +…+ +…
>> n=1:0.01:500; 2! 3 ! n!
>> semilogy(n,n.*log(n),'b',n,sqrt(n),'r',n,log(n),'g')
a)
b)
>> title('Big-O characteristics: Logarithmic Plot')
>> clear;
>> clear;
>> ylabel('Estimate of Running Time')
x = 2;
exp_APP = 0; % This is the exp approximation.
>> xlabel('n (number of elements)')
n = 0; x = 3; 2 −3 x
b) ∫ x e dx b) Find the general solution of the following
% Iterate until our approximation is below the error tolerance. fsecond order ODE:
>> syms x
while abs( exp_APP - exp(x) ) >= 0.001
d2 y dy
>> f = x^2*exp(-3*x);
2
+8 +16 y=0
exp_APP = exp_APP + x^(n)/factorial(n); dt dt
>> int(f)
n = n+1;
ans =
end
-(exp(-3*x)*(9*x^2 + 6*x + 2))/27 >> dsolve('D2y+8*Dy+16*y=0')
exp_Terms = n;
ans =
x
exp_Error = abs( exp_APP - exp(x) ); c) first and second derivatives of atan
5 C4*exp(-4*t) + C5*t*exp(-4*t)
% Output
>> syms x c) Find the particular solution in a) given the
fprintf ('\nNumber of Terms Needed for the function to be within
the allowed error:\n'); >> f = x/5;
following initial conditions:
fprintf ('-----------------------------------------------------------------------\n'); >> diff(f) y ( 0 )=5
fprintf ('exp(3): %g terms | Error = %g\n',exp_Terms,exp_Error); ans =

1/5
>> dsolve('Dy-y = exp(2*t)','y(0)=5')
Number of Terms Needed for the function to be within the >> diff(f,2)
ans =
allowed error:
ans =
exp(2*t) + 4*exp(t)
-----------------------------------------------------------------------
0
exp(3): 13 terms | Error = 0.000324362

2.2.2 2.3.2

1 a) Find the general solution of the following first


a) ∫ dx order ODE:
( x −1)(x−3)
dy
>> syms x
− y=e2 t
>> f = 1/(x-1)*(x-3);
dt
>> int(f)

ans = >> dsolve('Dy-y = exp(2*t)')

x - 2*log(x - 1) ans =

exp(2*t) + C3*exp(t)

You might also like