Sei sulla pagina 1di 4

1.

Plot the following


continuous-time signals using
MATLAB. If you decide that
the signal is periodic, select a
range for time t that will
cover at least 3 time periods.
(a) x(t) = 5 cos[ 2

( 15 )

t + 0.25 ]
(b) x(t) = 5 cos[ 2(15) t - 0.5
] + 5 cos[ 2(10) t + 0.3]
Please label all plots
clearly. Please include
program listing and
plots in your solution.
Solution:
% Problem 1(a)
clear
t=0:0.01:3;
x=5*cos(2*pi*15*t+0.
25*pi);
subplot(2,1,1);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('problem 1a');
% Problem 1(b)
x=5*cos(2*pi*15*t0.5*pi) +
5*cos(2*pi*10*t+0.3*
pi);
subplot(2,1,2);
plot(t,x);
xlabel('t');
ylabel('x(t)');
title('Problem 1b');
2. Plot the following discretetime signals using MATLAB.
If you decide that the signal is
periodic, select a range for
time t that will cover at least 3
time periods.
(a) x(n) = 0.5 |n| , in the range
-5 < n < 5
(b) x(n) = 5 cos( n + 0.3),
= 1.5 .
Please label all plots clearly.
Please include program listing
and plots in your solution.
Solution:
% Problem 2(a)
clear
n=-4.9:4.9;
x=0.5.^abs(n);
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('x(n)');
title('Problem 2a');
% Problem 2(b)
w=1.5*pi;
x=5 *cos(w*n +
0.3*pi);
subplot(2,1,2);
stem(n,x);
xlabel('n');
ylabel('x(n)');
title('Problem 2b');

4. There are two main forms of vector or matrix


multiplication. In MATLAB, if two vectors a
and b are given, then the two possible
MATLAB multiplication commands are: y =
a*b and y = a.*b.
i. Comment on the differences between these
two commands, and clearly state the outputs of
these two operations.
ii. Write a small MATLAB program to evaluate
these two commands for the case of a = [1 2 3]
and b = [4 5 6].
iii. Are both the operations a*b and a.*b
possible for the vectors given in (ii)? If not,
what change in the syntax would make the
operation possible?
Solution:
(i) The command y=a*b is a matrix
multiplication, and the requirement is that the
number of rows of matrix a should be equal to
the number of columns of matrix b.
The command a.*b is an element by element
product, and the requirement is that the size of
matrics a and b should be exactly the same, i.e.
the number of rows of matrix a should be equal
to the number of rows of matrix b, and number
of columns of matrix a should be equal to the
number of columns of matrix b.
(ii) % Problem 4 (ii)
clear
a=[1 2 3];
b=[4 5 6];
c=a*b; % Product multiplication
d=a.*b; % element by element
multiplication
c Error using * Inner matrix dimensions
must agree.
(iii) The command c=a.*b is fine, since both
matrices are identical in size; however, the
command c=a*b gives an error, since the
number of rows of a is not equal to the number
of columns of b. The corrected command would
c=a*b.

% Problem 1
% Problem 2
clear
n=[0.035 0.020 0.015 0.030 0.022];
P=100000; %Principal
s=[0.0001 0.0002 0.0010 0.0007 0.0003];
borrow amount, dollars
b=[10 8 20 24 15];
i=6/100; %Annual interest h=[2 1 1.5 3 2.5];
rate
u=sqrt(s).*(b.*h./(b+2*h)).^(2/3)./n;
% n is year index
plot(n,u);
1,2,3,4,5.
% A is the annual payment
n=1:5;
i1=(1+i).^n;
F=P*i1;
table(n,F,'VariableNames
',{'n' 'F' });

3. A number of image files can be accessed from the


MATLAB directory, or from the internet. Download
the image into a JPEG file into your local PC
directory using the following command:
>> y = imread ('filename.jpg') ; loads the image into
the matrix y
and the command:
>> imagesc(y) ; plots the image in a new window
>> colorbar ; attaches a color scale to the figure
Access any one two-dimensional image file from the
directory, example: coneplot.jpg, light_ex2.jpg,
surface_ex2.jpg.
(a) In each case print the corresponding image.
(b) Each image is stored as a matrix
a(m,n). Determine the size of the matrix
for each image, and determine the number
of pixels (m x n) in each case.
Solution:

clear
y = imread('ngc6543a.jpg');
imshow(y);
ymin=min(min(min(y)))
ymax=max(max(max(y)))
ysize=size(y)
number_of_pixels=ysize(1)*ysize(
2)
%%%%%%%%%
ymin=0
ymax=255
ysize=6506003

% Problem 3
% Relative error = (True value - approximate value)/True value
clear
x=5;
true = exp(-x);
% true value
% Calculation of approximate value using first series
approx1=0.0;
for i = 1:20;
ii=(-1)*(i+1);
% to create the alternating negative sign in series
approx1=approx1+ii*x^(i-1)/factorial(i-1);
true_error1= true-approx1
rel_error1=(true-approx1)/true
end;
% Calculation of approximate value using second series
approx2=0.0;
for i = 1:20;
approx2=approx2+x^(i-1)/factorial(i-1);
true_error2= true-1/approx2
rel_error2=(true-1/approx2)/true
end;
% Problem 4
clear
A=0.15;
e=0.90;
sigma=5.67e-8;
T=650;
Tl=650-20;
Tu=650+20;
Radiation_rate = A*e*sigma*T^4;
Radiation_ratel = A*e*sigma*Tl^4;
Radiation_rateu = A*e*sigma*Tu^4;
error_l=Radiation_rate- Radiation_ratel;
error_u=Radiation_rate- Radiation_rateu
'the true error will lie between',error_l,'and',error_u
Tl=650-40;
Tu=650+40;
Radiation_rate = A*e*sigma*T^4;
Radiation_ratel = A*e*sigma*Tl^4;
Radiation_rateu = A*e*sigma*Tu^4;
error_l=Radiation_rate- Radiation_ratel;
error_u=Radiation_rate- Radiation_rateu

Apolynomialfunctionisgivenby:
y(x)=x48x33.5x2+450x1001
a
Howwouldyouapproachtheproblem
of finding the roots of the function?
Create a Howmanyrootsdoyouexpect?
linearly spaced array of
b
Write a brief Matlab program to
points
determinetherootsofthefunction.
start:diffval:limit
where start is the first value in the
%Problem1
array,
clear is the difference between
diffval
P=[183.54501001];
successive
values in the array, and
roots(P)
limit
is the boundary for the last value
>>1:0.6:3
ans = 1.0000 1.6000 2.2000
2.8000
>>linspace(0, 1, 6)
ans =
0 0.2000 0.4000 0.6000 0.8000
1.0000
title('Plot of v versus t')
xlabel('Values of t')
ylabel('Values of v')
grid
hold on and hold off
subplot(m, n, p)

n = input('promptstring')
n = input('promptstring', 's')
month = input(enter month,1-12 )
day = input(enter day, 1-31 )
year = input(enter year,20xx )
today_date=[month day year]
disp(value)
%d - integer format
%e - scientific format with lowercase e
%E - scientific format with uppercase
E
%f - decidmal format
%g - the more compact of %e or %f
\n - start a new line
\t - tab
\\ - print the \ character

True error = y - yapprox


Absolute error = |y yapprox|
True relative error = (y - yapprox)/y
Relative error = ( y yapprox )/y x 100
y

Thecosinefunctioncanbeevaluatedbythefollowinginfiniteseries:

cos( x) 1
a
b

x2 x4 x6

....
2! 4! 6!

Howwouldyouapproachtheproblemtoevaluatethevalueofcos(x)uptothefirst10terms.
Whatcouldyouexpectasthenumberoftermsincreases?
WriteabriefMatlabprogramtoevaluatethepercentageerror,givenby:

% Error

True value - Approx. value (up to 10 terms)


* 100
True value

foravalueofx=0.5.
%Problem2
clear
x=0.5
true_value=cos(x);
approx_value=0.0;
fori=1:10;
approx_value=approx_value+x^(2*(i1))*(1)^(i1)/factorial(2*(i1));
3. Three kinds of material metal, plastic and rubber are required for production. The amounts required
to produce each component are shown in the table below:
Componen
t
1
2
3

Metal, g/component

Plastic, g/component

Rubber, g/component

15
17
19

0.25
0.33
0.42

1.0
1.2
1.6

Write a Matlab program to find how many of each component can be produced per day, if totals of 2.12,
0.0434 and 0.164 kg. of metal, plastic and rubber, respectively, are available each day.\
Let the number of units of components 1,2 and 3 be x,y and z respectively.
15x + 17y + 19z = 2120
0.25x + 0.33y + 0.42z = 43.4
1.0x + 1.2y + 1.6z = 164
[A][p] = [B]
% Problem 3
clear
a = [15 17 19;0.25 0.33 0.42;1.0 1.2 1.6];
b=[2120;43.4;164];
p1=inv(a)*b;
p2=a\b;
p3=linsolve(a,b);

y = @(x) x^4*(sin(x)
+4*(cos(x))^2)
x1 =fzero(y,1)

2. Use least-squares regression to fit


a straight line to the data:
X: 0 2 4 6 9 11 12 15 17 19
Y: 5 6 7 6 9 8 7 10 12 12
Plot the data and the line fit.
Matlab program
% Problem 2
clear
x=[0 2 4 6 9 11 12 15 17 19];
y=[5 6 7 6 9 8 7 10 12 12];
p = polyfit(x, y, 1);
x1=0:0.1:19;
y1=polyval(p,x1);
plot(x,y,o,x1,y1)
xlabel(x)
ylabel(y)
title(Problem 17.3)
4a.
Write a Matlab program to calculate
the integral:
2

(x

3 y 2 xy3 )dxdy

2 0

% Problem 4a
clear
N=100;
dx=(4-0)/N;
dy=(2-(-2))/N;
for i=1:N;
x(i)=0+(i-1)*dx;
for j=1:N;
y(j)=-2+(j-1)*dy;
z(i,j)=x(i)^2-3*y(j)^2+x(i)*y(j)^3;
end;
end;
I1=trapz(z,1);
I2=trapz(I1);
I=I2*dx*dy;
I=
1.1269e+004
4b.
Write a Matlab program to calculate
the differential:

Write a Matlab program to calculate


theintegral:
%Problem3
2

(x
1

1
) dx
x2

x=1:0.01:2;
y=x+(1./x.^2);
I=trapz(x,y)
2. (a) Write a Matlab program to
calculate the integral:
2

I [ x 2 x sin( x )]dx
1

(b) In the Matlab program, add


labels along the x and y axes,
and put a title of your own
choice.
Solution
% Problem 2
clear
x=1:0.01:2;
y=x.^2+x.*sin(x);
I=trapz(x,y);
plot(x,y);
xlabel(x);
ylabel(y);
title(problem 2);
3. Given the following twodimensional function:
f(x,y) = -8x +
x2 + 12y + 4y2 -2xy

Write a Matlab program to find the


minimum of the function, with a
starting initial condition of x0 = 0; y0
= -1.
Solution
% Problem 3
clear
d
1
I [( x 2 )], in the interval - 10 x 10; f=@(x)8*x(1)+x(1)^2+12*x(2)+4*x(2)^2
dx
x
-2*x(1)*x(2)
[x, fval] = fminsearch(f, [0, -1])
% Problem on differentiation
clear
x=-10:0.01:10;
y=x+1./x.^2;
I=diff(y)./diff(x);
plot(x,y)
hold
plot(I)

Potrebbero piacerti anche