Homework 1 Tarea 1
Homework 1 Tarea 1
Homework 1 Tarea 1
% shortProblems.m
% Homework 1, problems 1 through 7
% 1a
a) a=10;
% 1b
b) b=2.5e23;
% 1c
c) c=2+3*i; %2+3*j is also ok
% 1d
d) d=exp(j*2*pi/3); % i instead of j is also ok
d. dVec = [10 10 ⋯ 10 10 ] (logarithmically spaced numbers between 1 and 10, use logspace, make
0 0.01 0.99 1
% 2a
aVec=[3.14 15 9 26];
% 2b
bVec=[2.71; 8; 28; 182];
% 2c
cVec=5:-0.2:-5;
% 2d
dVec=logspace(0,1,101); % there are 101 values here, not 100
% can also do dVec=10.^(0:.01:1);
% 2e
eVec='Hello';
1 0 ⋯ 0
0 ⋱ 0 ⋱
b. bMat = ⋮ 0 5 0 9x9 matrix of all zeros, but with the values
0 ⋱ 0 ⋱
(⋯ 0 ⋮ 1)
1 10 ⋯ 91
2 12 ⋱ 92
c. cMat = ⋮ ⋮ ⋱ 0 10x10 matrix where the vector 1:100 uns down the
⋮ ⋱ ⋱ ⋱
(10 20 ⋯ 100)
f. Make fMat be a 5x3 matrix of random integers with values on the range -3 to 3 (use rand and
floor or ceil)
% 3a
a) aMat=2*ones(9); % or aMat=2+zeros(9);
% 3b
b) temp=[1:5 4:-1:1];
bMat=diag(temp,0);
% 3c
c) cMat=reshape(1:100,10,10);
% 3d
d) dMat=nan(3,4);
% 3e
e) eMat=[13 -1 5; -22 10 -87];
% 3f
f) fMat=floor(rand(5,3)*7)-3; % should be *7 not *6,
because of the floor
% fMat=ceil(rand(5,3)*7)-4; is also ok.
% 4a
a) x=1/(1+exp(-(a-15)/6));
% 4b
b) y=(sqrt(a) + b^(1/21))^pi;
% 4c
c) z=log(real((c+d)*(c-d))*sin(a*pi/3))/(c*conj(c));
% 5a
d) xVec=1/(sqrt(2*pi*2.5^2))*exp(-cVec.^2/(2*2.5^2));
% 5b
e) yVec=sqrt((aVec').^2+bVec.^2);
% 5c
f) zVec=log10(1./dVec);
6. Matrix equations. Using the variables created in 2 and 3, solve the equations below. Use matrix
operators.
b. yMat ii = bVec aVec, note that this is not the same as aVec bVec
T
c. zMat = cMat ( i ) , where aMat bMat cMat is the determinant of cMat , and T
again indicates the transpose (use det).
% 6a
a) xMat=(aVec*bVec)*aMat^2;
% 6b
b) yMat=bVec*aVec;
% 6c
c) zMat=det(cMat)*(aMat*bMat)';
% 7a
a) cSum=sum(cMat,1);
% 7b
b) eMean=mean(eMat,2);
% 7c
c) eMat(1,:)=[1 1 1];
% 7d
d) cSub=cMat(2:9,2:9);
% 7e
e) lin=1:20;
lin(2:2:end)=-lin(2:2:end);
% 7f
f) r=rand(1,5);
inds=find(r<0.5);
r(inds)=0;
8. Plotting multiple lines and colors. In class we covered how to Open a script and name
plot a single line in the default blue color on a plot. You may have it twoLinePlot.m. Write
noticed that subsequent plot commands simply replace the the following
existing line. Here, we’ll write a script to plot two lines on the same commands in this
axes. a.
script.
b. Make a new figure using figure
c. We’ll plot a sine wave and a cosine wave over one period
i. Make a time vector t from 0 to 2π with enough samples to get smooth lines
ii. Plot ( )sin t
iii. Type hold on to turn on the ‘hold’ property of the figure. This tells the figure
not
to discard lines that are already plotted when plotting new ones. Similarly, you
can use hold off to turn off the hold property.
iv. Plot ( )cos t using a red dashed line. To specify line color and style, simply
add a
third argument to your plot command (see the third paragraph of the plot
help).
This argument is a string specifying the line properties as described in the help
file. For example, the string ‘k:’ specifies a black dotted line.
d. Now, we’ll add labels to the plot
i. Label the x axis using xlabel
ii. Label the y axis using ylabel
iii. Give the figure a title using title
iv. Create a legend to describe the two lines you have plotted by using legend
and
passing to it the two strings ‘Sin’ and ‘Cos’.
If you run the script now, you’ll see that the x axis goes from 0 to 7 and y goes
e.
from -1 to
1. To make this look nicer, we’ll manually specify the x and y limits. Use xlim to
set the x
axis to be from 0 to 2π and use ylim to set the y axis to be from -1.4 to 1.4.
Run the script to verify that everything runs right. You should see something
f.
like this:
% twoLinePlot
% a plot that has two lines in two different colors
% make a new figure
figure
% make a time vector
t=0:.01:2*pi;
% plot a sine wave
plot(t,sin(t))
% hold on and plot a cosine wave on top of it in a red dashed line
hold on
plot(t,cos(t),'r--')
% label everything
xlabel('Time (s)');
ylabel('Function value');
title('Sin and Cos functions');
legend('Sin', 'Cos');
xlim([0 2*pi]);
ylim([-1.4 1.4]);
Screen output:
ans =
meanGrades =
meanGrades =
meanMatrix =
ans =
% seriesConvergence
% this script plots two series to verify that they converge to the
% analytical value
% define the constants
p=0.99;
k=0:1000;
% calculate each term in the series
geomSeries=p.^k;
% calculate the infinite sum
theoreticalValue=1/(1-p);
% plot theory and cumulative sum
figure
plot([0 max(k)],theoreticalValue*ones(1,2),'r');
hold on
plot(k,cumsum(geomSeries));
xlabel('Index');
ylabel('Sum');
title(['Convergence of geometric series with p=' num2str(p)]);
legend('Infinite sum','Finite sum');
% define the new constants
p=2;
n=1:500;
% calculate each term in the p-series
pSeries=(1./n).^p;
% calculated theoretical answer, which is the solution to the basel
problem
baselValue=pi^2/6;
% plot theory and cumulative sum
figure
plot([1 max(n)],baselValue*ones(1,2),'r');
hold on
plot(n,cumsum(pSeries));
xlabel('Index');
ylabel('Sum');
title('Convergence of p-series with p=2');
legend('Infinite sum','Finite sum');