0% found this document useful (0 votes)
19 views3 pages

Sheet-02 EPM486

Uploaded by

highman6000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Sheet-02 EPM486

Uploaded by

highman6000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Sheet 2

Programming Using MATLAB


And Numerical Applications

(1) Suppose that x = 6. Find the results of the following operations by hand and
use MATLAB to check your results.
(a) z = (x<10) (b) z = (x = = 10)
(c) z = (x>= 4) (d) z = (x ~ = 7)

(2) For the arrays x and y given below, use MATLAB to find all elements in x
that are greater than the corresponding elements in y.
x = [-3 0 0 2 6 8] and y = [-5 -2 0 3 4 10]

(3) The array price given below contains the price in dollars of a certain stock
over 10 days. Use MATLAB to determine how many days the price was above
20$.
Price = [19 18 22 21 25 19 17 21 27 29]

(4) The array price_A, price_B and price_C given below contain the price in
dollars of three stocks over 10 days.
(a) Use MATLAB to determine how many days the price of stock A was
above both the price of stock B and the price of stock C.
(b) Use MATLAB to determine how many days the price of stock A was
above either the price of stock B or the price of stock C.

(5) Rewrite the following statements to use only one if statement.


if x < y
if z < 10
w=x*y*z
end
end

(6) An ideal diode is used in a half wave rectifier circuit as shown in the figure.
The voltage VL across the load RL is given by:
VL = V S if VS > 0 and VL = 0 if VS = 0
Suppose that the supply voltage is
VS = 3 e-t/3 sin (π t) volts where t is in seconds.
Write a MATLAB program to plot the voltage VL versus t for t varies from 0 to 10
seconds.

Diode

VS RL VL

1
(7) The following m.file is a function file (linspace) for the creation of linearly
spaced vector. The program does not work properly due to at least four
mistakes. Find them then rewrite the program with errors after correction.
linspace.m
linspace (d1,d2,n)=y;
% linspace is linearly spaced vector
% linspace (x1,x2) generates a row vector
of 100 elements linearly
% equally spaced points between x1 and x2
%
% linspace (x1,x2,n) generates n points between
x1 and x2
% see also LOGSPACE
if nargin = 2
n = 100;
end
y=[d1+(0:n-2)*(d2-d1)/(n-1) d2];
end
return

(8) Use a loop in MATLAB to determine how long it take to accumulate


1,000,000 $ in a bank account if you deposit 10,000 $ initially and 10,000 $ at
the end of each year, the account pays 6 % annual interest.

(9) The equations describing the circuit shown in the figure are:
I1 I2 I3
- V1 + R 1 I 1 + R 4 I 4 = 0 R1 R2 R3
- R4 I4 + R2 I2 + R5 I5 = 0 I4 I5
+ +
- R5 I5 + R3 I3 + V2 = 0 R4 R5
V1 V2
I1 = I 2 + I 4 – –
I2 = I 3 + I 5

The given values for the resistance and voltages are:


R1 = 5 Ω, R2 = 100 Ω, R3 = 200 Ω, R4 = 150 Ω and R5 = 250 Ω
V1 = 100 volts and V2 = 50 volts.
Form the given data in the form of matrices and the use MATLAB to solve that
linear equations to get the current in each branch.

(10) Consider the following script. Fill in the lines of the table shown below with
the values that would be displayed immediately after the while statement if
you run the script file. Write in the values of the variables each time the while
statement is executed. You might need more or fewer lines in the table. Then
use MATLAB to check your answer.
K = 1; b = -2; x= -1; y = -2;
while k<=3

2
k, b, x, y
y = x^2 - 3;
if y<b
b = y;
end
x = x +1;
k = k +1;
end
Pass k b X y
First
Second
Third
Fourth
Fifth

(11) Write a program to calculate one solution for the equation


(x + x5 – 5 )1/2 / (x3 – 6 ) = 0
Using any technique.

(12) Write a program to calculate :


X = 1/a – 1/2a + 1/3a – 1/4a + ………… – 1/100a
For values of a = -5, -3, -1, 1, 3, …50

(13) Without using loops, generate the vector


x = [1 1/2 1/3 1/4 1/5 1/6 … 1/100].

(14) Generate a matrix that each element is a function of its number of row and
number of column as follow
A (i , j) = i3 + j3 .

You might also like