0
votes

I want to plot some vectors starting at a specific point 55 degrees north and 20 degrees east and follow a direction according to a random angle (generate with randn). I thought about doing it with a loop but that did not work out :

for i=1:100
    a=50+ 20.*randn;
    b = [a];
    i = i + 1;
    route = [20,50] + b * 
    plot(route, 'color', 'magenta')
    hold on
end

»»» route = [20,50] + b * i tried it like this because for me it looks like a stupid linear equation of the type of y = a+ bx ... i just don't know what to use for x... Also this way it will plot only 1 route and I need 100...

(So I need on one graph hundred vectors starting from the same point where the only parameter that varies is the direction)

Hope someone can help me. Any ideas?

ps: i am just starting matlab.

1

1 Answers

2
votes

1 - Try the following code for one line with direction:

%Initial line information
startPoint = [20 50] ;
direction  = [4 3] ;
lineLength = 100;

%Initialize line points with zeros
x  = zeros(lineLength);
y  = zeros(lineLength);

% Update line points
for i = 1 : lineLength
    x(i) = startPoint(1) + direction(1) * i;
    y(i) = startPoint(2) + direction(2) * i;
end

%Plot the line
plot( y , x ,'r.');

2 - And if you want the direction to be changing for every point,

use the following code:

%Initial line values
startPoint = [20 50] ;
lineLength = 100;

%create random direction vector
randomMax  = 100;
direction  = randi(randomMax,lineLength,2);

%Initialize line points with zeros
x  = zeros(lineLength);
y  = zeros(lineLength);

%set first points
x(1) = startPoint(1);
y(1) = startPoint(2);

% Update line points accumulating on previous point
for i = 2 : lineLength
    x(i) = x(i - 1) + direction(i,1) * i;
    y(i) = y(i - 1) + direction(i,2) * i;
end

%Plot the line
plot( y , x ,'r.');

3 - For various line each with different direction use the following code:

%Initial line values
startPoint = [20 50] ;
lineLength = 100;

%create random the 100 directions vector
randomMax  = 100;
directions = randi(randomMax,lineLength,2);

%Initialize line points with zeros
x  = zeros(lineLength,100);
y  = zeros(lineLength,100);

%set first points
x(1) = startPoint(1);
y(1) = startPoint(2);

h3 = figure;

% Update line points accumulating on previous point
for k = 1 : 100
    for i = 2 : lineLength

        x(i,k) = x(i - 1,k) + directions(k,1) * i;
        y(i,k) = y(i - 1,k) + directions(k,2) * i;

    end
    %hold the figure and plot the k-th line
    hold on;    
    plot( y(: , k) , x(: , k) , 'r.');
end