Connecting dots in a plot
11 views (last 30 days)
Show older comments
Hi, I need to connect the dots of the plot I'm getting. But when I try to replace the 'x' with '-' I don't see the line in the plot. I tried also to have the line in various colors but but with not luck so far.
Any help is appreciated, see code below:
Thanks.
function [] = HW3_Main_Code()
clc;
clear all;
gama=2.8;
Hmo=2;
% Constants definitions:
time=20; % Measurement time
f1=0.02;
f=f1:f1:time;
eta=1:f1:time;
Tp=5.45*sqrt(Hmo); % By correlation of wave measurements around Israel
alpha=0.064/(0.23+0.0336*gama-0.185/(1.9+gama)); % Normalization factor which is calculated to satisfy the relation Hmo=4*sqrt(mo)
for t=1:time
if f(t)<(1/Tp) sigma=0.07;
else sigma=0.09;
end
S=alpha*((Hmo^2)/(Tp^4))*((f(t))^(-5))*exp(-(5/4)*((Tp*(f(t)))^(-4)))*gama^(exp(-(1/2*sigma^2)*((Tp*(f(t))-1)^2)));
a=sqrt(2*S*f1);
phi=2*pi*rand(1,1); % random phase between 0 and 2*pi
omega=2*pi*f(t);
eta(t)=a*cos(omega*t+phi);
plot(t,eta(t),'x');
title('\eta - Real Sea Water Surface','Fontsize',8); ylabel('\eta [m]','Fontsize',8); xlabel('t [sec]','Fontsize',8);
hold on
end
0 Comments
Answers (1)
Star Strider
on 18 May 2020
Try this:
gama=2.8;
Hmo=2;
% Constants definitions:
time=20; % Measurement time
f1=0.02;
f=f1:f1:time;
% eta=1:f1:time;
Tp=5.45*sqrt(Hmo); % By correlation of wave measurements around Israel
alpha=0.064/(0.23+0.0336*gama-0.185/(1.9+gama)); % Normalization factor which is calculated to satisfy the relation Hmo=4*sqrt(mo)
tv=1:time;
for k = 1:numel(tv)
t = tv(k);
if f(t)<(1/Tp) sigma=0.07;
else sigma=0.09;
end
S=alpha*((Hmo^2)/(Tp^4))*((f(t))^(-5))*exp(-(5/4)*((Tp*(f(t)))^(-4)))*gama^(exp(-(1/2*sigma^2)*((Tp*(f(t))-1)^2)));
a=sqrt(2*S*f1);
phi=2*pi*rand(1,1); % random phase between 0 and 2*pi
omega=2*pi*f(t);
eta(k)=a*cos(omega*t+phi);
% plot(t,eta(t),'x');
% title('\eta - Real Sea Water Surface','Fontsize',8); ylabel('\eta [m]','Fontsize',8); xlabel('t [sec]','Fontsize',8);
% hold on
end
figure
plot(tv,eta,'-x');
title('\eta - Real Sea Water Surface','Fontsize',8); ylabel('\eta [m]','Fontsize',8); xlabel('t [sec]','Fontsize',8);
I created the ‘tv’ vector from ‘t’ and eliminated the first definition of ‘eta’ defining it only in the loop, and moved the plot call outside (after) the loop .
2 Comments
Star Strider
on 18 May 2020
My pleasure!
I essentially explained everything already, in the sentence after the code I posted.
Providing more detail, I eliminated the original definition of ‘eta’ (that was causing problems), then created ‘tv’ from the original ‘t’ vector and then created inidividual instances of ‘t’ from it in each iteration of the loop, using each of those to calculate an element of ‘eta’. The code then plotted both vectors (‘tv’ and ‘eta’) after the loop completed.
Plotting vectors rather than individual values allows connecting lines, because the plot function connects pairs of points with lines (if that is desired), the reason that plotting individual points within the loop failed to connect them with lines. Plotting individual points in each iteration provides no point-pairs to connect, so no lines appear between them.
See Also
Categories
Find more on Axis Labels 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!