필터 지우기
필터 지우기

Connecting dots in a plot

조회 수: 12 (최근 30일)
Anton Dor
Anton Dor 2020년 5월 18일
댓글: Star Strider 2020년 5월 18일
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

답변 (1개)

Star Strider
Star Strider 2020년 5월 18일
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
Anton Dor
Anton Dor 2020년 5월 18일
Thank you, it works, can you please explain further what you did?
What is numel?
Star Strider
Star Strider 2020년 5월 18일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by