# TritonED

Problem 1:

Download the file matA.mat from TritonED and load it into MATLAB. The file contains a 2-dimensional matrix matA. Use MATLAB functions rather than inspection to answer the following questions.
a) Does matA have equal column sums? Put the answer into a logical number p1a.
b) Does matA have equal row sums? Put the answer into a logical number p1b.
c) Is any element in matA a prime number? Put the answer in a logical number p1c.
Explore function isprime.
d) Which elements in matA are the prime numbers? Use linear indexing to report the
answer in a column vector p1d.

Problem 2:

A two-dimensional spiral is defined by the following expressions:
x = 0.2 cos(θ) e0:1 θ
y = 0.2 sin(θ) e0:1 θ
for 0 ≤ θ ≤ 8π.
(a) Create a vector theta to include values from 0 to to 8π with a consecutive difference
of 0:05π. Use the expressions above to obtain the values for vectors x and y. Create figure
1 and use function plot with the vectors x and y to plot the spiral. Use red solid line and
red circle symbols to mark the spiral.

(b) Estimate the arc length of the spiral. Approximate the arc length with straight lines
between consecutive points.
Problem 3:

A three-dimensional spiral is defined by the following expressions:
x = 0.2 cos(θ) e0:1 θ
y = 0.2 sin(θ) e0:1 θ
z = 0.1 θ
for 0 ≤ θ ≤ 8π.

(a) Create a vector theta to include values from 0 to to 8π with a consecutive difference
of 0:05π. Use the expressions above to obtain the values for vectors x, y and z. Create
figure 2 and use function plot3 with the vectors x, y and z to plot the spiral. Use magenta
solid line and magenta diamond symbols to mark the spiral.

(b) Estimate the arc length of the three-dimensional spiral. Approximate the arc length
with straight lines between consecutive points.

Problem 4:

The built-in function clock returns a row vector that contains 6 elements: the
first three are the current date (year, month, day) and the last three represent the current
time in hours (24 hour clock), minutes, and seconds. The seconds is a real number, but all
others are integers. Use function sprintf to accomplish the following formatting exercises.
a) Get the current date and time and store them in p4a. The current date and time
should be the date and time when the grader calls your script.

1. b) Using the format ‘MM:DD:YYYY’, write the current date to string p4b. Here, MM,
DD, and YYYY corresponds to month, day, and year, respectively.
2. c) Using the format ‘HH:MM:SS.SS’, write the current time to string p4c. Here, HH,
MM, and SS.SS corresponds to hour, minute and second, respectively.
3. d) Remove the last 3 digits from the string in part (c) so that the format is now
‘HH:MM:SS’. Put the answer into string p4d.
4. e) Combine the strings in part (b) and part(d) together separated by a single space. Put

Problem 5:

Create the following matrix B: ) Write the first column of matrix B into string p5a using %f with 5 characters including
3 decimal places for each element. The string must show a column vector.
b) Write the first row of matrix B into string p5b using %f with 5 characters including
3 decimal places for each element. The string must show a row vector.
c) Write matrix B into string p5c using %f with 6 characters including 2 decimal places.
d) Write matrix B into string p5d using %e with 10 characters including 2 decimal places.

Problem 6:

Download the file temperature.dat from TritonED. The file includes the San Diego
monthly-averaged temperature data from 1875 to 2014. The data is comma delimited and
the columns represent the following: Year, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep,
Oct, Nov, Dec, Annual mean. Load this data file into MATLAB.
a) Extract the years into row vector p6a.
b) Extract the temperatures during the months of October into row vector p6b.
c) Use function mean with the result in part (b) to compute the average temperature
for the months of October. Create a row vector p6c with the same length as the vector in
part (a) but all elements have the average temperature value.
d) During the months of October, what are the maximum and minimum temperatures?
Put the answers in a 2-element row vector p6d where the first element is the maximum and
the second is the minimum.
e) For the months of October, during which years do the maximum and minimum temperatures occur? Put the answer in a 2- element row vector p6e where the first element
is the year with the maximum temperature and the second is the year with the minimum
temperature.
f) Create figure 3. The figure should include the following:
• A line plot showing the temperatures for the months of October, use black solid line.
• A line plot showing the average temperature for the months of October, use blue solid
line.
• The maximum temperature for the months of October, use red square marker.
• The minimum temperature for the months of October, use green diamond marker.
• Give the figure a title and a legend, and label the axes.
g) Extract the temperatures for the year 1990 (from Jan to Dec) into row vector p6g.
h) Create row vector p6h to indicate the months from Jan (1) to Dec (12).
i) Create figure 4 with a bar graph showing the monthly temperatures for the year 1990.
The figure should include a title and labels for the axes.
j) Extract the temperature data from year 1990 to year 2000 (all columns including the
year) into matrix p6j and save the matrix into a new file named temperature 1990 2000.dat
using ASCII format.

Solution

%% Before running code, Set Path of your folder where the files are saved.

% for example “C:\Users\Name\Desktop\hw3”

%% Problem 1

% Part(a)

% for column sum

A=sum(matA);

p1a = all(A==A(1))

% Part(b)

% for row sum

A=sum(matA,2);

p1b = all(A==A(1))

% Part(c)

p1c = ismember(1,isprime(matA))

% Part(d)

p1d = find(isprime(matA))

%% Problem 2

%Part(a)

theta=0:0.05*pi:8*pi;

x=0.2*cos(theta).*exp(0.1*theta);

y=0.2*sin(theta).*exp(0.1*theta);

p2a=’See figure 1′

figure(‘Name’,’p2a’);

plot(x,y,’ro-‘);

title(‘Spiral’)

xlabel(‘x’);

ylabel(‘y’)

%Part(b)

leng_straight=0;

n=length(x);

for i=1:1:n-1

dx=x(i+1)-x(i);

dy= y(i+1)-y(i);

len=sqrt(dx^2+dy^2);

leng_straight=leng_straight+len;

end

p2b=leng_straight

%% Problem 3

%Part(a)

theta=0:0.05*pi:8*pi;

x=0.2*cos(theta).*exp(0.1*theta);

y=0.2*sin(theta).*exp(0.1*theta);

z=0.1*theta;

p3a=’See figure 2′

figure(‘Name’,’p3a’);

plot3(x,y,z,’md-‘);

title(‘3D Spiral’)

xlabel(‘x’);

ylabel(‘y’);

zlabel(‘z’)

%Part(b)

leng_straight=0;

n=length(x);

for i=1:1:n-1

dx=x(i+1)-x(i);

dy=y(i+1)-y(i);

dz=z(i+1)-z(i);

len=sqrt(dx^2+dy^2+dz^2);

leng_straight=leng_straight+len;

end

p3b=leng_straight

%% Problem 4

c=clock;

%Part(a)

p4a=sprintf(‘%d-%d-%d,\t%d:%d:%.2f\n’,c)

%Part(b)

p4b=sprintf(‘%d:%d:%d’,c([2 3 1]))

%Part(c)

p4c=sprintf(‘%d:%d:%.2f’,c([4 5 6]))

%Part(d)

p4d=sprintf(‘%d:%d:%2.0f’,c([4 5 6]))

%Part(e)

p4e=sprintf(‘%d:%d:%d\t%d:%d:%2.0f’,c([2 3 1 4 5 6]))

%% Problem 5

B=[pi pi^(1/4) pi^(1/7);pi^(1/2) pi^(1/5) pi^(1/8);pi^(1/3) pi^(1/6) pi^(1/9)];

%Part(a)

p5a=sprintf(‘%.3f\n%.3f\n%.3f’, B(:,1))

%Part(b)

p5b=sprintf(‘%.3f  %.3f  %.3f’, B(1,:))

%Part(c)

p5c=sprintf(‘%06.2f  %06.2f  %06.2f\n%06.2f  %06.2f  %06.2f\n%06.2f  %06.2f  %06.2f’,B’)

%Part(d)

p5d=sprintf(‘%010.2e  %010.2e  %010.2e\n%010.2e  %010.2e  %010.2e\n%010.2e  %010.2e  %010.2e’,B’)

%% Problem 6

% Part(a)

p6a=temperature(:,1)’

% Part(b)

p6b=temperature(:,11)’

% Part(c)

Mean=mean(p6b)

p6c=temperature(:,14)

% Part (d)

p6d=[max(p6b) min(p6b)]

% Part (e)

p6e=[temperature(find(p6b==max(p6b)),1) temperature(find(p6b==min(p6b)),1)]

% Part (f)

p6f=’See figure 3′

figure(‘Name’, ‘p6f’);

plot(temperature(:,1), temperature(:,11),’k-‘);

hold on;

plot(temperature(:,1), mean(temperature(:,11))*ones(length(temperature(:,11)),1));

hold on;

plot(temperature(find(p6b==max(p6b)),1),max(p6b),’rs’);

hold on;

plot(temperature(find(p6b==min(p6b)),1),min(p6b),’gd’);

title(‘Temperature of months of Octuber’);

xlabel(‘Year’);

ylabel(‘Teperature of Octuber’);

legend(‘Temp’, ‘Avg Temp’, ‘Max Temp’, ‘Min Temp’)

% Part(g)

p6g=temperature(find(temperature(:,1)==1990),2:13)

% Part(h)

p6h=1:12

% Part (i)

p6i=’See figure 4′

figure(‘Name’, ‘p6i’)

bar(p6g)

title(‘Bar plot for temperature on months of year 1990’)

xlabel(‘Month’)

ylabel(‘Temperature’)

% Part(j)

format short

p6j=temperature(find(temperature(:,1)==1990):find(temperature(:,1)==2000),:)

save(‘temperature_1990_2000.dat’,’p6j’,’-ascii’);