How do I connect data points with a line?

8 views (last 30 days)
Byeongchan Min
Byeongchan Min on 25 Apr 2020
Answered: Pravin Jagtap on 28 Apr 2020
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 Comment
dpb
dpb on 25 Apr 2020
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...

Sign in to comment.

Answers (1)

Pravin Jagtap
Pravin Jagtap on 28 Apr 2020
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.

Categories

Find more on 2-D and 3-D Plots 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!