In: Computer Science
Example 2:
Now suppose we want to plot one period of this periodic sine
function:
We could try to remember the rules for the shape of sine curves based on the general format y=d+sin(bx+c). This is best, but do you remember? What we can do without having to remember
is to plot this formula over a few different viewing windows until we can figure it out. Here is an example with comments
Figure 2: Different viewing windows to frame one period of a sine function.
>> x=linspace(0,2*pi); plot(x,50+30*sin(2*pi*x/365)) %% [0,2pi] is always a good start, but it is too small. Try a larger domain >> x=linspace(0,100); plot(x,50+30*sin(2*pi*x/365)) %% Still too small. Be bold let's see try 1000 >> x=linspace(0,1000); plot(x,50+30*sin(2*pi*x/365)) %% Oops, too big now >> x=linspace(0,365); plot(x,50+30*sin(2*pi*x/365)) %% just right. Oh yeah T=(2*pi/b)!
That doesn't take too long, as we only need to change one number and rerun the line. To take advantage of the "up arrow" trick, we put the commands on one line and combined the function evaluation with the plot function.
Exercise 3a:
Find the suggested values by plotting the graphs until you can
figure out the answer.
(This is a bit tricky! Keep plotting until you clearly get one period shown in the viewing window.)
Script:
clc;close all;clear all;
figure;
d=1;b=2*pi/20;c=0.5;
subplot(321)
x=linspace(0,500); plot(x,d+30*sin((b*x)+c));grid;
xlabel('t');ylabel('x(t)')
subplot(322)
x=linspace(0,100); plot(x,d+30*sin((b*x)+c));grid;
xlabel('t');ylabel('x(t)')
subplot(323)
x=linspace(0,50); plot(x,d+30*sin((b*x)+c)) ;grid;
xlabel('t');ylabel('x(t)')
subplot(324)
x=linspace(0,25); plot(x,d+30*sin((b*x)+c));grid;
xlabel('t');ylabel('x(t)')
subplot(325)
x=linspace(0,20); plot(x,d+30*sin((b*x)+c));grid;
xlabel('t');ylabel('x(t)')
title('x(t) for one period by evaluating the above 4 plots')
figure;
T=2*pi/b;
x=linspace(0,T); plot(x,d+30*sin((b*x)+c)) ;grid;
xlabel('t');ylabel('x(t)');title('x(t) for one period exactly from the formulae')