How to make 3D-line plots (Sine Wave)?

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.
sinewaves.png

9 Comments

‘I tried bunch of methods but I couldn't get it.
If you post your code, we can help you get it working correctly.
close all;
figure;
t = 0:0.01:5*pi;
y = sin(t);
z = 0:0.01:5*pi;
plot3(t,y,z);
I only get one wave and my wave plot doesn't look like the one in the picture. I thought maybe I can put inside the for loop then I could get bunch of plots but I couldn't write the code.
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.)
t = 0:0.01:5*pi;
z = 0:pi/12:2*pi;
for a = 1:0.2:4
phase = 0
y = a .*sin(t + phase);
phase = phase + pi/10;
disp(y)
end
axes()
hold on
for i = 1:numel(z)
plot3(t,z(i)*ones(size(t)),y);
end
I used the answer below for help too, but still could'nt graph the way in the picture.
@Hamid Kilic, you're 99.9% there! Simply add the last line of my answer to your code.
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.
I can not see amplitude change and phase shift. This is the output :
sine.png
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.
Thanks, you have given me so many useful tips.
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.

Sign in to comment.

 Accepted Answer

With three small changes to your code, it works:
t = 0:0.01:5*pi;
z = 0:pi/12:2*pi;
phase = 0; % Initiliise Outside The Loop
a = 1:0.2:4; % Assign Outside The Loop
for k = 1:numel(a)
y(k,:) = a(k)*sin(t + phase);
phase = phase + pi/10;
% disp(y)
end
axes()
hold on
for i = 1:numel(z)
plot3(t,z(i)*ones(size(t)),y);
end
The changes are with respect to ‘phi’ and ‘phase’, and adding the ‘k’ loop counter and subscript.
My solution:
t = linspace(0, 4*pi);
a = (1:25);
sinmtx = 0.2*a(:).*sin(t + a(:)*pi/10);
figure
plot3(t, a(:)*ones(size(t)), sinmtx)
grid on
view(35,45)

More Answers (1)

Adam Danz
Adam Danz on 21 Nov 2019
Edited: Adam Danz on 21 Nov 2019
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!