How to make 3D-line plots (Sine Wave)?
Show older comments
Hi everybody, I am new to MATLAB and I need a help with this problem.
Plot a series of sine functions which are phase shifted by pi/10 and whose amplitudes are increased by 0.2. Final result is going to be like this. I tried bunch of methods but I couldn't get it. Thanks in advance.

9 Comments
Star Strider
on 21 Nov 2019
‘I tried bunch of methods but I couldn't get it.’
If you post your code, we can help you get it working correctly.
Hamid Kilic
on 21 Nov 2019
Edited: Hamid Kilic
on 21 Nov 2019
Star Strider
on 21 Nov 2019
Put it inside the loop. Increment the amplitude and phase in each iteration, saving each vector as a row of a 2D matrix. The ‘t’ vector will be the same for all of them.
Post your solution, and I will post mine.
(I solved it without any loops.)
Hamid Kilic
on 21 Nov 2019
view(12.6, 27.6)
Your plot is actually 3D but you're only looking at the (x,y) plane. That line rotates your plot so you see it in 3D.
Then you have to adapt the data to fit the requirements of the assignment.
Hamid Kilic
on 21 Nov 2019
Adam Danz
on 21 Nov 2019
I just looked at your code more carefully (previously I just looked at the figure it produced). There are some problems that I'll address individually.
1) I recommend not using this style of loops: for a = 1:0.2:4
Instead, define 'a' as a vector and use integers as your loop variable. That would look something like this, below. The following tips assumes you've made this change.
a = 1:0.2:4 ;
for i = 1:numel(a)
%[code omitted]
y = a(i) .*sin(t + phase);
%[code omitted]
end
2) In your loop you are defining phase as 0: phase = 0 so there will be no phase shifts. Move the phase outside of the loop and define it as a vector that has 1 value for each iteration of the loop.
3) Most importantly, you aren't saving the y-values within the loop! They are being overwritten on each iteration. disp(y) only displays the y-values in the command window. That slows down the code and should be removed. To save the y values on each iterations,
y = nan(numel(t), numel(a)); % each column will contain data from 1 iteration
for i = 1:numel(a)
%[code omitted]
y(:,i) = a(i) .*sin(t + phase); %store in column i
%[code omitted]
end
Those are the next steps to take. Use those tips to apply additional changes you'll need to make to get the script working. Examine each variable. Is it the size that you are expecting? Does it have values that make sense? If you get stuck again, follow up with another comment but try to get a few steps further than the advice already provided.
Hamid Kilic
on 21 Nov 2019
Adam Danz
on 21 Nov 2019
Glad I could help - I think you would have gotten there on your own which is a great learning process. Take time to understand each line of the answer that Star Strider gave to you so you can own the assignment and understand why it works.
Accepted Answer
More Answers (1)
Since this seems like homework, my solution below shows how to produce the figure but you'll need to adapt it to comply with the requirements of the assignment.
t = 0:0.01:5*pi;
y = sin(t);
z = 0:pi/12:2*pi;
clf()
axes()
hold on
for i = 1:numel(z)
plot3(t,z(i)*ones(size(t)),y);
end
grid on
xlabel('t')
ylabel('o.5 pi') % Replace with ylabel(['0.5',char(960)]) for pi symbol
zlabel('sin(t)')
view(12.6, 27.6)

Categories
Find more on Graphics Performance 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!