You are on page 1of 3

Exercise (Lab Report 2)

1. Write a code in a script name labreport2_q1 to do the following tasks:


Lets x=[1:0.1:10] and y=10*sin(x). Use for and if command to clip the y values so that y values
that are larger than 5 to be 5 , the y values that are smaller than -5 to be -5 and the y value
between -5 and 5 is remain the same. Save the clip y value in z variable. Plot the new z variable
against x and y against x in the same graph. Label the graph correctly.
Answer:
x= [1:0.1:10];
y=10*sin(x);
plot(x,y,'r*')
hold on;
for i=1:91
if y(i) > 5
y(i) = 5
elseif y(i) < -5
y(i) = -5
else;
end
end
xlabel('x')
ylabel('y=10*sin(x)')
title('y and y(i) against x')
plot(x,y,'b+')
legend('y','z')

y and y(i) against x

10

y
z

y=10*sin(x)

-2

-4

-6

-8

-10

10

2. Write a code in a script name labreport2_q2 to do the following tasks:


a) Create a code to load the wavelength.txt and spectrometerdata.txt into the MATLAB.
b) Spectrometer data consists of 91 rows of spectrum data with varied input power. Create a code
using for command to automatically plot the spectrum data against wavelength for spectrum data
at row 1 (power=1mW), row 31(power=2mW), row 61(power=3mW) and row 91(power=4mW)
on the same graph. Label the graph appropriately.
Answer:
load newspectrometerdatarev.txt
load wavelength.txt
x=wavelength
for i=1:30:91
y1=newspectrometerdatarev(1,:)
y61=newspectrometerdatarev(31,:)
y31=newspectrometerdatarev(61,:)
y91=newspectrometerdatarev(91,:)
end
plot(x,y1,'ro')
hold on;
plot(x,y31,'g+')
plot(x,y61,'b*')
plot(x,y91,'k.')
xlabel('wavelength')
ylabel('spectrometer data')
title('spectrometer data against wavelength of various power input')
legend('power=1mW','power=2mW', 'power=3mW', 'power4mW')

x 10

spectrometer data against wavelength of various power input


power=1mW
power=2mW
power=3mW
power4mW

spectrom eter data

0
1000

1100

1200

1300

1400
1500
wavelength

1600

1700

1800

3. Write a code in a script name labreport2_q3 to do the following tasks:


a) Create a code to load the powerdependence.txt into the MATLAB.
b) Delete the 1st and 2nd column when the 2nd column data contains 0 (zero) using for and if
command.
Answer:
load powerdependence.txt
x= powerdependence;
for j= x(':,2');
if (':,2')
x(sum((x==0),2)>0,:)=[]
end
end

You might also like