Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

2018 Spring Semester

ME 349 Engineering Analysis


Homework, Part 4
Assigned: 02/02/18
Due: 02/9/18
Student: Saleh Alajmi CWID: 11836775

Problem 1

Part-a:

Without matlab:

First addition and subtraction will be performed:

a+b = 8;

a-b = 4;

c = -5;

y = 8>4<-5

so going from left to right:

8>4 =1

1<-5 = 0

Answer is y = 0 .

Code:

a = 6;
b = 2;
c = -5;
y_a = a+b>a-b<c
Output:

Part-b:

Without matlab:

y = -6<-5<-2
From left to right:

-6<-5 = 1

1<-2 = 0

Answer is y = 0

Code:
a = 6;
b = 2;
c = -5;
y_b = -6<c<-2
Output:

Part-c:

Without matlab:

First division:

a/b = 3

Now addition and subtraction:

b+c = -3

Now, greater than:

c> a/b = 0

Now, finaly greater than and equal to:

Y = b+c >=0

Answer is 0 .

Code:
a = 6;
b = 2;
c = -5;
y_c_1 = a/b
y_c_2 = b+c
y_c_3 = c>a/b
y_c_4 = b+c >= c>a/b
Output:

Part-d:

Without matlab:

First Parenthesis:

c+a ~=a/b-b

Now in this parenthesis first division will be operated:

a/b = 3

Now addition and subtraction:

C+a = 1 and a/b-b = 1

Now not equal to operator will be tested:

1 ~= 1 answer is 0

Now the result of this parenthesis will be negated:

~(0) =1

Now addition and subtraction:

A+c = 1;
Now equal condition will be checked:

1 == 1

So, Final answer is 1.

Code:
a = 6;
b = 2;
c = -5;
y_d = a+c==~(c+a~=a/b-b)

Output:

Problem 2

Part-a:

Without matlab:

0&21

Answer is 0 .

Code and output:

Part-b:

Without matlab:
~-2>-1 & 11>=~0

Answer is y = 1

Code and Output:

Part-c:

Without matlab:

4-7/2 & 6<5|-3

Answer is y = 1

Code and Output:

Part-d:

Without matlab:

First negation:

~2 = 0;

Then multiplication:

0*-3 = 0

Now and operator will be applied:

-1&0 = 0

Now or operator will be applied:

-3|0|0 = 1

So answer is 1.
Code and Output:

Problem 3:

Code:

Main code:

x = [-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5]


[P,N] = two_vec_splitter(x)

Function:

function [P,N] = two_vec_splitter(x)


count = size(x);
n =1;
P = [];
N = [];
p_i = 1;% index for P
n_i =1;% index for N
while n<=count(2)
if x(n) >=0
P(p_i) = x(n);
p_i = p_i +1;
else
N(n_i) = x(n);
n_i = n_i +1;
end
n=n+1;
end

Output:

Problem 4:

Code:
TCH = [75 79 86 86 79 81 73 89 91 86 81 82 86 88 89 90 82 84 81 79 73 69 73
79 82 72 66 71 69 66 66]
TSF = [69 68 70 73 72 71 69 76 85 87 74 84 76 68 79 75 68 68 73 72 79 68 68
69 71 70 89 95 90 66 69]
%% Part-a Average of temperature
sprintf('PART-A')
no_days = size(TCH);
n = 1;
sum_CH = 0;
sum_SF = 0;
while n <= no_days(2)
sum_CH = sum_CH + TCH(n);
sum_SF = sum_SF + TSF(n);
n = n+1;
end
avg_CH = sum_CH/no_days(2)
avg_SF = sum_SF/no_days(2)

%% Part-b Temp above average


sprintf('PART-B')
n = 1;
day_abov_avg_CH = 0;
day_abov_avg_SF = 0;
while n <= no_days(2)
if TCH(n)> avg_CH
day_abov_avg_CH = day_abov_avg_CH + 1;
end
if TSF(n) > avg_SF
day_abov_avg_SF = day_abov_avg_SF +1;
end
n = n+1;
end
sprintf('The number of days the temp was above average in Chicago')
day_abov_avg_CH
sprintf('The number of days the temp was above average in SF')
day_abov_avg_SF

%% Part-c Temp comparison b/w CH and SF


sprintf('PART-C')
n = 1;
days_CH_SF = 0;
dates = [];
dates_count = 1;
while n <= no_days(2)
if TSF(n)< TCH(n)
days_CH_SF = days_CH_SF+1;
dates(dates_count) = n;
dates_count = dates_count+1;
end
n= n+1;
end
sprintf('The number of days the temp in SF<CH')
days_CH_SF
sprintf('In the following dates')
dates

%% Part-d Temp same in both cities


sprintf('PART-D')
n = 1;
days_same = 0;
dates_same = [];
dates_count = 1;
while n <= no_days(2)
if TSF(n)== TCH(n)
days_same = days_same+1;
dates_same(dates_count) = n;
dates_count = dates_count+1;
end
n= n+1;
end
sprintf('The number of days the temp was same')
days_same
sprintf('In the following dates')
dates_same
% End of code
Output:
Problem 5:

Code:

Main code:

m_1 =5;
m_2 = 10;
m_3 = 20;
sprintf('With m = 5')
[out] = pi_func(m_1)
format long
actual = pi
sprintf('With m = 10')
[out] = pi_func(m_2)
actual = pi
sprintf('With m = 20')
[out] = pi_func(m_3)
actual = pi

Function:

function [out] = pi_func(m)


constant = sqrt(12);
n = 0;
sum = 0;
while n <= m
sum = sum + (-1/3)^n/(2*n+1);
n = n+1;
end
out = constant * sum;
Output:

You might also like