Matlab drawing surf function Color Control parameter C usage

Source: Internet
Author: User

Specialized in MATLAB this year (civil engineering matlab?) , the teacher gave the title, in order to not Hang branch is also bitter forcing Baidu + Google a half-day, plus their own pondering, finally is understand some surf color control method, here to share record ~ Beginner matlab, understand is not too deep, hope Haihan.

Topic

FromExcelparticles, which have three-dimensional coordinates,XYZ, and has a three-dimensional speed,ExcelStore An example of a point in time in each page of thex,y,Z,VX,VY,VZ, the page is namedSheet+i, want to be able to read the particle information into the vector, and then the position of the particle image display, the particle is displayed by the sphere, the color of the sphere and the size of the speed of the ball, using a gradient from green to red, then the particles in three-dimensional space, according to the change of time movement.

Description : I want the time step to be the input parameter of the function, that is, how many points of time are controllable, the starting point is controllable

From my many years of programming, well, the logic is very simple, but the animation this piece is more egg pain, after all, have not touched the MATLAB animation ~ so began to search the online related tutorials, these things are still quite a lot of. It then creates the code that generates a ball and makes it move, and then hold on to draw multiple balls.

Then is the color control, the first idea is to use ColorMap, after the use of the later found to be set to cover the previous, that is, all the balls are the same color,,.

After continuing to search, found surf (x,y,z,c) The fourth parameter is the color control,, but search a lot of is said to be color control, is not to say exactly how to control, this is more awkward.

So in debugging and search, found that the error message CDATA must be a m*n matrix or m*n*3 array, and C is the default value of Z,, then have to go to Lenovo, surf function is to use the grid plot, c corresponding to each grid color, That is, each grid can be color-set using RGB. Then set A and z the same size of the matrix, and then upgrade to three-dimensional array, the third dimension (column) corresponding to the RGB tri-color , passed to the surf function, then just achieve the color control. So the test (below is the code snippet, no context)

[x, Y, z] = Sphere (n);

C=zeros (Size (x));% get o array size and x same

For I=1:1:length (c (1,:))
For J=1:1:length (C (:, 1))
C (i,j,1) = 1;
C (i,j,2) = 0;
C (i,j,3) =0;% Red
End
End

No problem, all the balls are red.

Then the problem is solved.

function Anim=ani2 (numb,start)
dian=[];
v=[];
I=1;
vmax=0;
vmin=100;
While 1
lsp=xlsread (' dian.xlsx ', i);
if Length (LSP) ==0
Break ;
End
LSP (1) =LSP (1) +LSP (4) *start;
LSP (2) =LSP (2) +LSP (5) *start;
LSP (3) =LSP (3) +LSP (6) *start;
DIAN=[DIAN;LSP (1), LSP (2), LSP (3)];
V=[V;LSP (4), LSP (5), LSP (6)];
lsv=sqrt (LSP (4) *LSP (4) +LSP (5) *LSP (5) +LSP (6) *LSP (6));
if Lsv>vmax
VMAX=LSV;
End
if Lsv<vmin
VMIN=LSV;
End
i=i+1;
End

k = 5;
n = 2^k-1;
[x, Y, z] = Sphere (n);
For I=1:1:length (Dian (:, 1))
Surf (X+dian (i,1), Y+dian (i,2), Z+dian (i,3));
Hold on ;
End
axis Equal
Axis Tight
set (GCF, ' renderer ', ' zbuffer ');
set (GCA, ' nextplot ', ' Replacechildren ');
title (' Sphere Animation ');
j_color=vmax-vmin;
For J=1:1:numb
k = 5;
n = 2^k-1;
[x, Y, z] = Sphere (n);
set (GCA, ' nextplot ', ' Replacechildren ');
For I=1:1:length (Dian (:, 1))
Dian (i,1) =dian (i,1) +v (i,1);
Dian (i,2) =dian (i,2) +v (i,2);
Dian (i,3) =dian (i,3) +v (i,3);
g=1-(sqrt (V (i,1) *v (i,1) +v (i,2) *v (i,2) +v (i,3) *v (i,3))-vmin)/j_color;
r=1-g;
C=zeros (Size (z));
For Ll=1:1:length (C (1,:))
for J=1:1:length (C (:, 1))
C (ll,j,1) =r;
C (ll,j,2) =g;
C (ll,j,3) =0;
End
End
Surf (X+dian (i,1), Y+dian (i,2), Z+dian (i,3), c);
Hold on ;
End
axis Equal
F (j) =getframe;
End

Hope to use surf when you are helpful ~

Matlab drawing surf function Color Control parameter C usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.