Mary A. Marion
Mary A. Marion

Reputation: 800

repmat function in Matlab does not work consistently?

Matlab functions meshgrid and streamline are not matching. How to fix?

%% direction field plot
[x,y]=meshgrid(-4:.5:4,-4:.5:4);       
dx = 2*x +1*y;
dy = 1*x +2*y;
dxu = dx./sqrt(dx.^2+dy.^2);
dyu = dy./sqrt(dx.^2+dy.^2);
quiver(x,y,dxu,dyu)
hold on  

%% Trajectory Plot
startx = repmat([-4:.5:4], 0,2); 
starty = ones(size(startx));
streamline(x,y,dxu, dyu, startx, starty)
dxu = dx./sqrt(dx.^2+dy.^2);
dyu = dy./sqrt(dx.^2+dy.^2);

print('c:\data\DirectionField','-dmeta')
saveas(gcf, 'c:\data\streamline.emf')
hold off

Error messages are given below:

Error using repmat!
Replication factors must be a row vector of integers or integer scalars.

This has occurred when I added 7 trajectory plots to the code. When using only two trajectories the error did not occur? What is happening here?

MM

Upvotes: 0

Views: 546

Answers (1)

Savithru
Savithru

Reputation: 745

Your startx and starty matrices are currently empty. The last two arguments of repmat should be the number of times you want to repeat the matrix in the vertical and horizontal directions respectively. Since your replication factors are 0 and 2, the result is an empty matrix. Use positive integers for the replication factors.

I'm not completely sure what you're trying to do, but if you want the quiver and streamline plots to be consistent, I think you should not be using repmat at all. Instead, I think you should just do:

streamline(x, y, dxu, dyu, x, y); 

Updated after OP's comment:

If you want to want to plot trajectories from a specific set of starting points, use the code below where startxy is an m x 2 matrix containing the coordinates of m starting points.

startxy = [0,2;
           1,-3;
           2,1]; %e.g. 3 starting points
streamline(x,y,dxu, dyu, startxy(:,1), startxy(:,2));

Upvotes: 1

Related Questions