Friday, June 5, 2009

Plotting vectors

I received an e-mail from a reader, I feel happy with it, so if anyone needs help, I'm receiving asks.

So, let's follow with our Scilab's tutorial.

This post is about plotting vectors, like the picture.

This kind of plotting is very common for applications on electromagnetism or analysis of fluids.

In Scilab, we can use the champ(.) function for plotting vectors.

Look the following script:

-->x = -5:5;

-->y = 0:4;

-->[xv yv] = meshgrid(x, y)
yv =

0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3.
4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4.
xv =

- 5. - 4. - 3. - 2. - 1. 0. 1. 2. 3. 4. 5.
- 5. - 4. - 3. - 2. - 1. 0. 1. 2. 3. 4. 5.
- 5. - 4. - 3. - 2. - 1. 0. 1. 2. 3. 4. 5.
- 5. - 4. - 3. - 2. - 1. 0. 1. 2. 3. 4. 5.
- 5. - 4. - 3. - 2. - 1. 0. 1. 2. 3. 4. 5.

-->wx = %pi/3;

-->xv = cos(wx*xv);

-->yv = yv/max(yv);

-->champ(x, y, xv', yv', 1);

The result is showed following:

The champ(.) has five arguments, the first and the second are the coordinates of each vector, the third and the fourth are the components of each vector, and the fifth is a correction argument (for normalization).

Try to make graphs and say me what happens.


me said...

I was very happy to discover your blog!
This post was especially helpful to me, thanks a lot!!

Sheep said...

I'd like to help who needs help in Scilab.
Thanks for your comment.

Anonymous said...

great post.
any idea on how to color the vectors? i've seen some people do this in matlab.

Anonymous said...

To color the vectors (instead of vectors being proportional to the intensity of the field) use champ1 instead of champ.

Anonymous said...

hi... i know that this is an old post, but i just found it know... i would like to know that how to plot a vector from a raw data provided by an external data using scilab.

if you have any information on this please let me know. tq

Sheep said...

Hi last commenter, have you tried to use files?

I wrote posts about files here in this blog.

If it's not, I ask you try to be more explained.

Thanks for your comment.
God bless you.

KamilAbdullah said...

i am the last commenter,

i understand the "write" and "read" command available on the post but i have a problem to declare the vx value and vy value as available in the data file.

here some conclusion of mine, correct me if i am wrong:

1. declaring the x-axis range
2. declaring the y-axis range
3. declaring the vx and vy value
4. plot using champ(.)

waiting for your comments

KamilAbdullah said...

hi its me again.

i already capable of ploting my vector plot as base on your instruction. thank you.

i just want to know if is it possible to have an increment in the x and y value?

for example
--> x = -5:5;

means that -5,-4,....,4,5

is it possible for me to create x and y axis value to be in the order of 0,3,6,...,15...

i know it such a basic question for your but as a newbie i dont know this.

anyway thanks again.

Sheep said...

Hello Kamil, may I call you like this?

You can make all questions you have, no problem.

I'm happy because you solved your problem, and about the new sequence:

x = 0, 3, 6, ..., 15

you make the following code:

x = 0:3:15;

the number '3' between '0' and '15' is the step of the sequence.

If you make the code:

x = 0:15;

the step is 1 (default value).

Thanks for your comments and you are welcome to comment more.

KamilAbdullah said...

Thanks for your respond its really help. I will put more question rather than comments if you dont mind. :D

anyway glad to find someone willing to share knowledge like you.

KamilAbdullah said...

hi there, its me again,

i understand that the vector is automatically scale to the x-axis and y-axis range, does it?

my problem is that i have set of data which does not have the same interval; as in my previous question. the vector plot then become very small in which i can only see the arrow head and not the line.

do you know how to overcome this problem? hope to hear from you soon. tq in advance.

Sheep said...

Dear Kamil, try the function champ(.) like this:

champ(x, y, xv', yv', 0.5);

If it doesn't work, please, post your code and I'll try to solve your problem.

God bless you.

ruy699 said...

(sorry for my language faults, I am not english speaker)
Hi. I think this blog is great!

I tried these script:
Ex = (K*Q*x) ./ ((x^2 + y^2)^(3/2))
Ey = (K*Q*y) ./ ((x^2 + y^2)^(3/2))
champ (x,y,Ex,Ey)

But i obtain these error mesage:

champ: first and third arguments have incompatible dimensions.

Any idea about where is my mistake?

Sheep said...

Hi Ruy, I changed some things at your code, but I think you can understand, try to execute it:

x = .01:.01:1;
y = .01:.01:1;

[xv yv] = meshgrid(x, y);

Q = 10;
d = 0.1;
K = 9000000000;

Ex = (K*Q*xv)./((xv.^2 + yv.^2).^(3/2));
Ey = (K*Q*yv)./((xv.^2 + yv.^2).^(3/2));

champ(x, y, Ex, Ey);

The variables Ex and Ey must be matrices, so you may use meshgrid() function for transforming (x, y) in matrices for calculating Ex and Ey.

And other thing, look the dot '.' before the pow operator '^', it makes the operator being applied to each element.

ruy699 said...

Yes,Sheep, you are right. Thank you very much!

Shaista said...

I liked ur blog ...its quite helpful..
I am a beginnner ...n I have been trying to find out how to read columns from a file ..for a vector plot ..can u help?

Sheep said...

Hi everyone, I've seen some people are looking for plotting vectors from data in files.

This post: presents how to read columns from files and for plotting vectors, you can follow what is presented in the actual post.


Shaista said...

Thanks sheep!
but i still face problems ..
I have a file with four columns
To read the first column, i used:
-->y3 = read("test_data.dat", -1,1)
but what if I want to read all rows from just the second column?
If I use:
-->y3 = read("test_data.dat", -1,2)
it reads the first two columns ...
i need to assign different variables to different columns..
can u help?

Shaista said...

also if u could tell me the same for a 3d vector plot. .. in matlab one uses quiver and quiver3 for 2d and 3d vector plots. for scilab, now i know champ1 ..what abt 3d plots?

Sheep said...

For reading 4 columns from a file, I suggest you use it:

x = read("test_data.dat", -1, 4);

v1 = x(:,1); // first column

v2 = x(:,2); // second column

v3 = x(:,3); // third column

v4 = x(:,4); // fourth column

About other questions you have, I suggest you look the Labels, in the right side of the page.


vv said...


I have a question. my program returns me results for x and y components of position vector for each of 640 particles and vx, vy components of velocity for each of 640 particles (the problem is from fluid mechanics).
i have tried the following piece of code:
x=[-0.1720490 -0.1487229 -0.1305250 -0.1145734 -0.0985839 -0.0839435 -0.0688476 -0.0544547 -0.0397398 -0.0254066];
y=[-0.9771022 -0.9786739 -0.9800474 -0.9809699 -0.9819347 -0.9828030 -0.9835949 -0.9842851 -0.9848132 -0.9851994];
vx=[-0.0865696 -0.0932412 0.0501032 0.0070481 0.0198802 -0.0129471 -0.0303309 -0.0561734 -0.0786280 -0.1039486 ];
vy=[-0.0283387 -0.0447400 -0.0706520 -0.0859012 -0.0924338 -0.1009400 -0.1058732 -0.1108179 -0.1144853 -0.1172428];

champ1(x, y, vx, vy);

but it is giving me back error:

champ1(x, y, vx, vy);
!--error 999
champ1: first and third arguments have incompatible dimensions
at line 8 of exec file called by :
in execstr instruction called by :

what am I doing wrong? whole matrix contains 640 particles that I would like to draw, this is only test matrix with 10 entries. I think that either there is problem with matrix entries or maybe I should use another function?

thx for help :)

Sheep said...

Hi VV, thanks for commenting.

champ1() function works with four parameters: x, y, vx and vy.
The first two ones are vectors [1 x n] (or [m x 1]), and the two last ones are matrices [m x n], where x and y means the grid locations of the vectors and vx and vy means the vectors (with i and j components).

Look the example that I posted and see the variables after running.


Dima said...

Can you export champ-plots in eps format? If ``yes'', which version of Scilab do you use?

Sheep said...

Hi Dima. I think any version of Scilab can export graphs to eps format.
It's possible to created eps files through code, but it's easier to export by clicking on 'file' option in the graph window.