Question

I'm trying to use Matlab for some data plotting. In particular I need to plot a series of lines, some times given two points belonging to it, some times given the orthogonal vector.

I've used the following to obtain the plot of the line:

  1. Line given two points A = [A(1), A(2)] B = [B(1), B(2)]:

    plot([A(1),B(1)],[A(2),B(2)])
    
  2. Line given the vector W = [W(1), W(2)]':

    if( W(1) == 0 )
        plot( [W(1), rand(1)] ,[W(2), W(2)]) 
    else
        plot([W(1), W(1) + (W(2)^2 / W(1))],[W(2),0]) 
    end
    

    where I'm calculating the intersection between the x-axis and the line using the second theorem of Euclid on the triangle rectangle formed by the vector W and the line.

enter image description here

My problem as you can see from the picture above is that the line will only be plotted between the two points and not on all the range of my axis.

I have 2 questions:

  1. How can I have a line going across the whole axis range?
  2. Is there a more easy and direct way (maybe a function?) to plot the line perpendicular to a vector? (An easier and more clean way to solve point 2 above.)

Thanks in advance.

Was it helpful?

Solution

Do you know the bounds of your axis for displaying the plot? If so, you can specify the range of the plot with the axis([xmin, xmax, ymin, ymax]) function.

So, from your question, if you know the slope m and intercept b, you can make sure your function plots the line across the whole window by specifying:

plot([xmin, xmax], [m*xmin + b, m*xmax + b]);
axis([xmin, xmax, min(m*xmin+b, m*xmax+b), max(m*xmin+b, m*xmax+b)]);

where xmin and xmax are values you specify as the range of your x-axis. This will make your line go from the corner of your plot to the other corner. If you want a buffer in the y-direction, then add one like so:

buffer = 5; % for example, you set this to something that looks good.
axis([xmin, xmax, min(m*xmin+b, m*xmax+b)-buffer, max(m*xmin+b, m*xmax+b)+buffer]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top