How can I plot a sine wave with my data? I'm almost there but I'm stuck
Show older comments
Frequency = 1000 Hz
time = 0.001 sec
Amplitude = 5 V
I want to plot a sine wave for a lab. I want the x-axis (time) to go from -0.001 to 0.001.
The following is the code I tried running:
%PLOT SINE WAVE
f_1 = 1000; %frequency in Hertz
t_1 = 0.001; %time in seconds
A_1 = 5; %amplitude in volts
t = -0.001:0.001; %time (x-axis)
X = A_1 * sin(2*pi*t_1*f_1);
%END OF CODE
I looked online and tried to plot it, but my X value is a single number and not an array. This is where I got stuck. I appreciate any help.
Answers (1)
dpb
on 24 Oct 2013
t = -0.001:0.001; %time (x-axis)
There's your problem--the default interval for colon ':' is 1 -- hence the next interval is 0.999 > 0.001 and so you don't get a vector of times but only the first. Here's where linspace is very useful--you don't need to compute a dt explicitly for inconvenient values--
You have already defined a variable t_1; I'll use that to make it easier to modify the code by not burying constants--
t=linespace(-t_1,t_1,50); % generate 50 points between -/+ t_1
Now evaluate your X with that t and all will be well... :)
Categories
Find more on Surface Style in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!