How do I connect data points with a line?

조회 수: 31 (최근 30일)
Byeongchan Min
Byeongchan Min 2020년 4월 25일
답변: Pravin Jagtap 2020년 4월 28일
Hi
I made a quadratic iteration using 'for'. Below is my code;
a=2.95; x11=0.65; x12=-0.001;
for i=0:50;
y11=f(a,x11);
y12=f(a,x12);
i=i+1;
figure(1)
subplot(2,2,1)
title('alpha=2.95')
hold on
plot(i,y11,'-.')
hold on
plot(i,y12,'-d')
axis([0 50 -1 1])
x11=y11; x12=y12;
end
function f=f(a,x);
f=a*x*(1-x);
end
So I wanted to add a line connecting points (i,y11) and (i,y12) respectively.
Especially for points (i,y12), because it diverses to negative infinity too fast and I wanna show its divergeance only, instead of showing like all the way down to 10^-13 of y axis.
Hompage says to assign a linestyle using such like '-o' or '-.', but none of those gave me a line but only dots.
What should I do to get lines?!
  댓글 수: 1
dpb
dpb 2020년 4월 25일
You're only plotting a point a at time...save the computed points and then plot the arrays after the loop has calculated all the data is probably easiest...or save the previous and plot the new from the previous to the new point before overwriting it (the current) in the loop...

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

답변 (1개)

Pravin Jagtap
Pravin Jagtap 2020년 4월 28일
Hello Byeongchan,
It is not clear that what you want to achieve from your code. Especially, increment of iterator 'i' inside the for loop and the use 'subplot' is not clear. I am assuming that you want to plot a function y= f(x) and connect two points on that curve. Please refer to the following code and check whether you want to achieve the same:
% Inputs
a=2.95; x11=0.65; x12=-0.001;
% Generating X and Y
X = -1:0.01:1; % It can be chagened as per requirements
Y = 2.95.*X.*(1-X);
% Calculating function values at x11 and x12
y11=funVal(a,x11);
y12=funVal(a,x12);
% Generating data for line
XX = [x11 x12];
YY = [y11 y12];
% Plotting
plot(X,Y);
hold on;
plot(XX,YY,'-o');
% Function defination
function f=funVal(a,x)
f=a*x*(1-x);
end
Hope this will help.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by