how to draw line between points in Matlab (2024)

7 views (last 30 days)

Show older comments

Meriem Boukhaima on 9 Jan 2017

  • Link

    Direct link to this question

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab

  • Link

    Direct link to this question

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab

Edited: Niels on 9 Jan 2017

Accepted Answer: Niels

I have to plot a graph showing results in a for loop, the problem is that I only get points instead of a proper graph: here is my code:

%this program plots the number of iterations versus the

% errors in a bisection methode which is used to find he zero of a function.

clc

a=0;

b=2;

E0=[]

f=@(x)exp(-exp(-x))-x %the nonlinear function

for i=3:10

e=10^-i

n=ceil(log(b-a)-log(e)/log(2))

hold on

axis([10^-10 10^-3 0 12])

grid on

plot(e,n,'.')

for i=1:n

c=(a+b)/2;

if f(c)*f(a)<0

b=c;

else

a=c;

end

end

end

your help is Appreciated! Thank you

1 Comment

Show -1 older commentsHide -1 older comments

dpb on 9 Jan 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#comment_419000

Because points is all you asked for...

plot(e,n,'.')

Remove the '.' linestyle string

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Niels on 9 Jan 2017

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#answer_249902

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#answer_249902

Edited: Niels on 9 Jan 2017

if you want lines between the points you have to save the data in vectors and plot the vectors, not single points

i changed some lines in your codes, take a look

a=0;

b=2;

E0=[];

% since you know what size e and n will have you can define them here alrdy

[e,n]=deal(zeros(8,1))

f=@(x)exp(-exp(-x))-x; %the nonlinear function

for i=3:10

% index starts at 1

e(i-2)=10^-i;

n(i-2)=ceil(log(b-a)-log(e(i-2))/log(2));

hold on

axis([10^-10 10^-3 0 12])

grid on

% points can still be plottet

plot(e(i-2),n(i-2),'.')

% why i again, you used i inprevious loop

% dont use same variable for index of a loop in a loop

for j=1:n

c=(a+b)/2;

if f(c)*f(a)<0

b=c;

else

a=c;

end

end

end

% plot lines between the points

plot(e,n)

you should test this comman with other examples like

x=linspace(0,2*pi,100);

plot(x,sin(x))

2 Comments

Show NoneHide None

Meriem Boukhaima on 9 Jan 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#comment_419016

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#comment_419016

Thank you very much, I finally got the plot I want:

%This program plots the required number of iteration versus the error

clc

clear

a=0;

b=2;

for i=3:10

e(i-2)=10^-i;

n(i-2)=(log(b-a)-log(e(i-2)))/log(2);

hold on

axis([0 10^-3 0 30])

grid on

plot(e(i-2),n(i-2),'.')

end

plot(e,n)

how to draw line between points in Matlab (5)

I removed deal because I didn't know what it does, but it worked without it, can you explain what's the point of it?

Thank you a lot!!!

Niels on 9 Jan 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#comment_419070

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#comment_419070

Edited: Niels on 9 Jan 2017

If you look at your code e and n should be underlined in red. Read what matlab displays. It should be something like e and n changes size every ... so to start with e is not defined. Then its a 1x1 vector. Next iteration its 1x2 etc same for n. This slows down matlab but in your case its not rly worth mentioning. In general if the size is know you better define the variable. Deal(zeros...) sets the previous mentioned variables to a 8x1 vector, each entry 0

Sign in to comment.

More Answers (1)

Image Analyst on 9 Jan 2017

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#answer_249907

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#answer_249907

You didn't completely specify the line style. You can specify color, line style, and marker. For example to do a red solid line of width 2, with spot shaped markers of size 15:

plot(e, n, 'r.-', 'LineWidth', 2, 'MarkerSize', 15);

1 Comment

Show -1 older commentsHide -1 older comments

Meriem Boukhaima on 9 Jan 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#comment_419015

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/319629-how-to-draw-line-between-points-in-matlab#comment_419015

Thank you for your response! it's been helpful

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsDiscrete Data Plots

Find more on Discrete Data Plots in Help Center and File Exchange

Tags

  • plotting lines

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


how to draw line between points in Matlab (9)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

Europe

Asia Pacific

Contact your local office

how to draw line between points in Matlab (2024)
Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6137

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.