In: Computer Science
Section 1.0: MATLAB Fundamentals Step 1.1 Open the MATLAB application Step 1.2 In the Command Window, type the following: x=8 % percentage sign denotes comment y=9 % variable y set to 9 x+y % add variable x + y clear %clears the workspace window clc % clears the command window Note: the % sign indicates a user comment. In the Command Window, you should see your answer : ans=17 In the Workspace window, observe "ans", "x" and "y" Step 1.3 In the Command Window, type pi %this returns the value of Pi = 3.1416 2*pi % multiples pi by 2 Step 1.4. Carry Signal Representation: To represent a carrier, we can use either a sine or cosine: s(t)=A*sin(2πft ± φ), or s(t)=A*cos(2πft ± φ). Except for the phase angle difference of 90 degrees or π/2 radians, there are no other differences between the two equations. We will explore the phase angle difference. First we need to define the variables of the signal as a function of time (t): Define the variables clear clc A=5 % this is the signals peak amplitude f=100 % frequency in Hertz phase=0 %phase angle in radians t=2.5e-03 %time in seconds which is the variable s=A*sin((2*pi*f*t)+phase) %carrier signal equation Note: if you make an error in typing the equation, you can use the arrow up key which will bring up your last entry. You can then make a correction to the equation vice re-typing the entire equation. You should get the answer: s=5? Check this on your calculator. Does the answer on your calculator match MATLAB's? Note: If your result is s=1.37e-01, then your calculator is set for degrees and not radians. Remember, our signal equation is in radians, which means that our phase angle will also be in radians and not in degrees. 3 Step 1.5 Now you will define a time axis from which you will be able to plot a simple sine wave. In order to define the x axis, we must provide a starting point, the increments in-between the start and end points, and end time for the plot (t=0:0.0001:0.01)- i.e., plot starts at t=0 seconds, with a point plotted every 0.0001 seconds, and ends at t=0.01 sec. Enter the following: clc clear A=5 f=100 sphase=0 t=0:0.0001:0.01 %the colon separates beginning, increment and end points s=A*sin((2*pi*f*t)+sphase) subplot(1,2,1) %subplot allows you to place two graphs for comparison in a 1x2 matrix plot(t, s) %graphically plots the sine wave 's' You should see the following sine wave plot: Step 1.6 Now we compare the sine wave next to a cosine wave. Enter the following into the Command Window (note: the “clear” command resets all of the variables): clc clear A1=5 A2=5 f1=100 f2=100 sphase=0 bphase=0 t=0:0.0001:0.01 s=A1*sin((2*pi*f1*t)+sphase) subplot(1,2,1) plot(t, s) %Add the following code to plot the cosine function: b=A2*cos((2*pi*f2*t)+bphase) %carrier signal equation but using cosine instead subplot(1,2,2)%subplot allows you to place two graphs for comparison in a 1x2 matrix plot(t, b) %graphically plots the cosine wave 'b' 4 You should see the following sine wave plot: We see that the cosine wave on the right has shifted 90 degrees to the left. Another way to interpret this is by saying that the cosine wave , since it has a peak amplitude earlier than the sine wave,"leads" the sine wave by 90 degrees or +1.57 radians. Step 1.7 Now let's shift the cosine wave form, b(t), by -90 degrees, or -π/2 radians (entered as “-pi/2” in MatLab code): clc clear A1=5 A2=5 f1=100 f2=100 sphase=0 bphase=-pi/2 t=0:0.0001:0.01 s=A1*sin((2*pi*f1*t)+sphase) subplot(1,2,1) plot(t, s) b=A2*cos((2*pi*f2*t)+bphase) subplot(1,2,2) plot(t, b) Note: If using the MATLAB's command window vice script, you must execute the above command lines in the command window. If you are using the MATLAB scripting function, change the values within the code variables and re-run the script. You should see that both s(t) and b(t) appear the same - i.e., both look like sine waves. Since the cosine wave had to shift -90 degrees in phase in order to look like the sine wave, we can say that the cosine wave "leads" the sine wave by 90 degrees. We can also say that the sine wave "lags" the cosine wave by 90 degrees. 5 Step 1.8 Now let’s return the cosine phase angle back to 0 radians, and plot both the sine and cosine waves onto a single graph for comparison. The red plot is the cosine wave while the blue plot is the sine wave. The cosine plot leads the sine plot by 90 degrees or π/2 radians. (Note: Make sure to close the plot window before running the script!) clc clear A1=3 A2=5 f1=150 f2=150 sphase=0 bphase=0 t=0:0.0001:0.01 s=A1*sin((2*pi*f1*t)+sphase) b=A2*cos((2*pi*f2*t)+bphase) yyaxis left %this command places the y scale on the left side plot(t,s) ylim([-8 8]) ylabel('s=A1*sin((2*pi*f1*t)+sphase)') yyaxis right %this command places the y scale on the right side plot(t, b) ylim([-8 8]) ylabel('b=A2*cos((2*pi*f2*t)+bphase)') xlabel('Time (seconds)') title('Sine and Cosine Waveforms') 6 Step 1.9 Using the MatLab code in step 1.8, modify the sine sphase, by +90 degrees or +pi/2 radians, and keep the cosine wave, bphase, at 0 degrees phase angle. Also raise the amplitude of the sine wave to A1=5. Question 1.9 Select the best answer regarding your observation from step 1.9. a. both s(t) and b(t) appear as identical sine waves b. both s(t) and b(t) appear as identical cosine waves c. both s(t) and b(t) appear as cosine waves; however, their amplitudes differ d. s(t) appears as a cosine wave while b(t) appears as a sine wave Plot 1.9 - Sine Cosine Wave Plot Submission: Submit (i.e., copy/paste) the MATLAB plots from step 1.9 above into the “IT300 Virtual Lab Plot Submission” document template. Note: After executing the plot command, go to the plot and perform a "copy figure" under the "Edit" menu. Paste this figure into the assignment "IT300 Virtual Lab Plot Submission v13" on Bb Assignments/Virtual LAB folder. Step 1.10 Now plot the following carrier waves s(t) and b(t). Place both plots onto a single graph as you did in steps 1.8 and 1.9. (hint: note the form for sinusoidal wave, s(t)=Asin(2πft ± φ) and b(t)=Acos(2πft ± φ)) s(t) = 4sin(2π250t) b(t) = 5cos(π500t -π/2) Question 1.10 What are the differences between the two plots a(t) and b(t) from step 1.10? a. s(t) and b(t) have different frequencies b. s(t) and b(t) differ in phase angle, and both appear as cosine waves c. s(t) and b(t) differ in amplitude, and both appear as sine waves d. s(t) and b(t) are identical in amplitude, frequency, and phase angle 7 Plot 1.10 - Sine Cosine Wave Plot Submission: Submit (i.e., copy/paste) the MATLAB plots from step 1.10 above into the “IT300 Virtual Lab Plot Submission” . Step 1.11 Set the phase angles for s(t) to pi/2 radians, and b(t) phase to 0. Set the frequency for s(t) equal to 600 Hz and b(t) equal to 300 Hz (note: keep all other variables the same). Question 1.11 Select the correct observation for s(t) and b(t) a. plots are same in amplitude b. plots are same in frequency c. both plots appear as cosine waves with different frequencies d. all are correct Plot 1.11 - Sine Cosine Wave Plot Submission: Submit (i.e., copy/paste) the MATLAB plots from step 1.11 above into the “IT300 Virtual Lab Plot Submission” .
Step 1.2
>> x=8
x = 8
>> y=9
y = 9
>> x+y
ans = 17
Step 1.3
>> pi
ans = 3.1416
>> 2*pi
ans = 6.2832
Step 1.4
>> A=5
A = 5
>> f=100
f = 100
>> phase=0
phase = 0
>> t=2.5e-03
t = 0.0025000
>> s=A*sin((2*pi*f*t)+phase)
s = 5
Step 1.5
clc;clear;
A = 5
f = 100
t=0:0.0001:0.01;
sphase = 0
s=A*sin((2*pi*f*t)+sphase);
subplot(1,2,1)
plot(t,s)
step 1.6:
A1=5; A2=5; f1=100; f2=100; sphase=0; bphase=0;
t=0:0.0001:0.01;s=A1*sin((2*pi*f1*t)+sphase);
subplot(1,2,1); plot(t, s);
b=A2*cos((2*pi*f2*t)+bphase);
subplot(1,2,2)
plot(t, b)
step 1.7
clc ;clear;
A1=5; A2=5; f1=100; f2=100;
sphase=0;bphase=-pi/2;
t=0:0.0001:0.01;
s=A1*sin((2*pi*f1*t)+sphase);
subplot(1,2,1); plot(t, s) ;
b=A2*cos((2*pi*f2*t)+bphase);
subplot(1,2,2); plot(t, b) ;
step 1.8
A1=3; A2=5;
f1=150; f2=150;
sphase=0;
bphase=0;
t=0:0.0001:0.01;
s=A1*sin((2*pi*f1*t)+sphase);
b=A2*cos((2*pi*f2*t)+bphase);
yyaxis left ;
plot(t,s) ;
ylim([-8 8]); ylabel('s=A1*sin((2*pi*f1*t)+sphase)');
yyaxis right;
plot(t, b) ;ylim([-8 8]) ;ylabel('b=A2*cos((2*pi*f2*t)+bphase)')
xlabel('Time (seconds)') ;title('Sine and Cosine Waveforms')
step 1.9(plot)
Step 1.10
t=0:0.0001:0.01;
s=4*sin(2*pi*250*t);
b=5*cos((pi*500*t)-(pi/2));
yyaxis left ;
plot(t,s) ;
ylim([-8 8]); ylabel(' s=4*sin(2*pi*250*t)');
yyaxis right;
plot(t, b) ;ylim([-8 8]) ;ylabel('b=5*cos((pi*500*t)-(pi/2))')
xlabel('Time (seconds)') ;title('Sine and Cosine Waveforms')
step 1.11
t=0:0.0001:0.01;
s=4*sin((2*pi*600*t)+(pi/2));
b=5*cos(pi*300*t);
yyaxis left ;
plot(t,s) ;
ylim([-8 8]); ylabel(' s=4*sin(2*pi*250*t)');
yyaxis right;
plot(t, b) ;ylim([-8 8]) ;ylabel('b=5*cos((pi*500*t)-(pi/2))')
xlabel('Time (seconds)') ;title('Sine and Cosine Waveforms')