Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
what kind of plot ?

Subject: what kind of plot ?

From: Fero

Date: 13 May, 2011 16:03:05

Message: 1 of 15

I am newbie in Matlab, and I have to do some analyse, and the result should be a graph. I know what should i get, but i dont know how. Can anybody help me, and say what is this for a plot and how can I make it ? because i havent found anything that looks like this in help...

http://imghost.sk/share/359423-plot.jpeg

I will be thankfull for any advice.

Subject: what kind of plot ?

From: ImageAnalyst

Date: 13 May, 2011 16:32:05

Message: 2 of 15

Fero:
Can you digitize your data into a 2D array? Then you could display it
as an image like your example. If your data is not (yet) on a regular
grid, then use TriScatteredInterp (TriScatteredInterp is the
recommended alternative to griddata as it is generally more
efficient.)

If you want to mask the rectangular image with a circle, you can use
the FAQ:
http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
or ask us. I left it out to keep the code brief.

% Create some sample data.
z = 32 * peaks(400)+7;
% Display it.
image(z);
colormap(jet(256));
colorbar;
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
hold on;

% Place random text labels
for k = 1 : 10
    x = rand(1,1)*400;
    y = rand(1,1)*400
    % Plot the marker;
    plot(x, y, 'yo');
    % Plot the label
    theText = sprintf('Point %d at (%.1f, %.1f)',...
    k, x, y);
    text(x+5, y, theText, 'Color', 'y');
end
title('Plot for Fero', 'FontSize', 40);

Subject: what kind of plot ?

From: Fero

Date: 13 May, 2011 18:07:04

Message: 3 of 15

Thank you very much, this seems to be what i was looking for, now i realised that i havent my mathematics part good. I think that i have to convert from cartesian to polar coordinates in the next step. I found a way, but not for a matrix.
Is it possible for a matrix ?
And is the process with the graph the same then ?
I keep looking for it on the internet, but I will be thankfull for help. I need to finish this work till Monday, and i am totally lost in matlab :-D

Subject: what kind of plot ?

From: ImageAnalyst

Date: 13 May, 2011 18:08:09

Message: 4 of 15

Check out cart2pol() and pol2cart(). If that's not what you wanted,
write back.

Subject: what kind of plot ?

From: Fero

Date: 13 May, 2011 18:56:02

Message: 5 of 15

Thank you for your time, this is not what I was looking for, your advice was good, but i am few hours working on this, and i am not thinking some times :-X i dont need to convert it, my code is absolutely bad....i tried it some ways, please if you would be so kind and watch for them, and tell me which way is better or where i do mistakes...
this is what i should do :
http://imghost.sk/image/359443-formula.jpeg

this is my first code :

x=-1:0.005:1;
y=-1:0.005:1;
P=6E-4;
R=62.5e-3;
t=0.01;
beta1=((R-y).^2+x.^2).^2;
beta2=((R+y).^2+x.^2).^2;
sigma_x=(-2*P/(pi*t))*((x.^2).*(R-y)./(beta1.^2)+(x.^2).*(R+y)./(beta2.^2)-1/(2*R));
image(sigma_x)

but i realised, that this wont be a matrix with size (x,y), so i tried another way :

sigma=zeros(210,210);
a=0;
for x=-1:0.01:1;
    a=a+1;
    b=0;
    for y=-1:0.01:1;
        b=b+1;
P=10;
R=62.5e-3;
t=0.01;
beta1=((R-t)^2+a^2)^2;
beta2=((R+t)^2+a^2)^2;
sigma(a,b)=(-2*P/(pi*t))*((a^2)*(R-b)/(beta1^2)+(a^2)*(R+b)/(beta2^2)-1/(2*R));
    end
end
image(sigma)

but this also doesnt give me a good achievement, or i dont know how to use it... :-(
I know that i am bothersome, and maybe i want a lot from you, but i dont have time to study matlab better and do it on my own :-(

Subject: what kind of plot ?

From: ImageAnalyst

Date: 13 May, 2011 20:16:45

Message: 6 of 15

I have no idea how you got those terms. Where did you get those
formulas for beta1 and beta2? Why on earth do the betas it depend on
"a", which is simply a loop counter???

I know the indices for sigma should be a and b, but why do you have a
and b in the right hand side of the equation, instead of x and y like
your web site said?

Subject: what kind of plot ?

From: Fero

Date: 14 May, 2011 20:19:02

Message: 7 of 15

Hi,
i am back again, when I posted the codes i was tired and a posted bad code, sorry for that.
Now I think that i got it good realised in the maths form, and the achievement looks like the numbers i wanted. But i still dont know how to get graph like that :-( maybe its a bad way to do it like a matrix, but can you have a look on this code ?

sigma=zeros(1249,1249);
a=0;
for x=-0.0624:0.0001:0.0624;
    a=a+1;
    b=0;
    for y=-0.0624:0.0001:0.0624;
        b=b+1;
P=6E-4;
R=0.0625;
t=1;
beta1=((R-y)^2+x^2);
beta2=((R+y)^2+x^2);
sigma(a,b)=(-2*P/(pi*t))*((x^2)*(R-y)/(beta1^2)+(x^2)*(R+y)/(beta2^2)-1/(2*R));
    end
end

i will tell you why i used such a code step by step....
matrix is size (1249x1249) because with such a step that is used later it shoul be so size,
x and y are a little bit smaller then R, because R is a diameter of the object, and in beta if i caltulate it with the same size it is math error later, and it has no sense to calculate it outside the circle
a and b are indexes in the matrix, and this is what could be bad, because on the picture the axis are from -1 to 1
beta1 and beta2 just take as they are, i didnt want you to study the whole problem, i just need help :-(
When i run this m-file, i get a lot of good numbers, and some bad ( that could be outside the circle maybe ) and i have no idea how to draw such a nice graph like in your example. I was working on it today but nothing more comes to my mind today :-(
I would be thankful for any other help, or ideas....

Subject: what kind of plot ?

From: ImageAnalyst

Date: 14 May, 2011 20:53:29

Message: 8 of 15

Well I don't know about the numbers. If you say they're good for you
then fine. Just add this code after your code to visualize them

imshow(sigma, []);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
colormap(lines);
msgbox('done');

Subject: what kind of plot ?

From: Fero

Date: 15 May, 2011 20:54:04

Message: 9 of 15

I hoped, that there is a way how to draw the graph with the axis like in my picture, that there will be the axis from -1 to 1 with x/R and y/R and that it will be possible to draw there this kind of data from the matrix sigma(a,b).
Can you help me how to make such a graph with axis like I mentioned ?

Subject: what kind of plot ?

From: ImageAnalyst

Date: 15 May, 2011 22:15:53

Message: 10 of 15

On May 15, 4:54 pm, "Fero " <secur...@kinanitra.sk> wrote:
> I hoped, that there is a way how to draw the graph with the axis like in my picture, that there will be the axis from -1 to 1 with x/R and y/R and that it will be possible to draw there this kind of data from the matrix sigma(a,b).
> Can you help me how to make such a graph with axis like I mentioned ?

----------------------------------------------------------
Fero:
I don't think your code is giving the "spread" of the function like
your example, yours seems more compacted into the points near the
perimeter and doesn't spread out into the interior as much. In fact
there are large areas that don't change much as all, at least they
don't change by 1/256 so that the colormap isn't able to distinguish
them by color.

Nonetheless I did the best I could. Try the code below:
(Feel free to experiment around with the color map.)


% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
% function test1
% function test1()
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
sigma=zeros(1249,1249);
a=0;
for x=-0.0624:0.0001:0.0624;
    a=a+1;
    b=0;
    for y=-0.0624:0.0001:0.0624;
        b=b+1;
P=6E-4;
R=0.0625;
t=1;
beta1=((R-y)^2+x^2);
beta2=((R+y)^2+x^2);
sigma(a,b)=(-2*P/(pi*t))*((x^2)*(R-y)/(beta1^2)+(x^2)*(R+y)/
(beta2^2)-1/(2*R));
    end
end
imshow(sigma, []);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
colormap(lines);

minValue = min(min(sigma))
maxValue = max(max(sigma))

% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
imageSizeX = size(sigma, 2);
imageSizeY = size(sigma, 1);
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = imageSizeX/2;
centerY = imageSizeY/2;
radius = imageSizeX/2;
circlePixels = sqrt((rowsInImage - centerY).^2 ...
    + (columnsInImage - centerX).^2) <= radius;
% circlePixels is a 2D "logical" array.
% Now, display it.
% image(circlePixels) ;
% colormap([0 0 0; 1 1 1]);
% title('Binary image of a circle');

% Or, assign values outside the circle.
newGrayLevelOutside = minValue;
sigma(~circlePixels) = newGrayLevelOutside ;
% Transpose it to be like his figure.
sigma = sigma';
imshow(sigma, []);
axis on;
cMap = [gray(224);jet(32)];
cMap(1, :) = [0 0 0];
colormap(cMap);
colorbar;
title('Demo for Fero, by ImageAnalyst', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off');

% Set up tick marks.
numberOfTickMarks = 9
xTicks = linspace(1, imageSizeX, numberOfTickMarks);
yTicks = linspace(1, imageSizeX, numberOfTickMarks);
xTicksCal = linspace(-.0624,0.0624, numberOfTickMarks);
yTicksCal = linspace(-.0624,0.0624, numberOfTickMarks);
set(gca,'XTick', xTicks);
set(gca,'YTick', yTicks);
for tickMark = 1 : numberOfTickMarks
caTickLabelsX{tickMark} = sprintf('%8.5f', xTicksCal(tickMark));
caTickLabelsY{tickMark} = sprintf('%8.5f', yTicksCal(tickMark));
end
set(gca,'XTickLabel',[caTickLabelsX]);
set(gca,'YTickLabel',[caTickLabelsY]);
grid on;

msgbox('done');

Subject: what kind of plot ?

From: Roger Stafford

Date: 15 May, 2011 23:57:02

Message: 11 of 15

"Fero " <security@kinanitra.sk> wrote in message <iqmo3m$f1j$1@newscl01ah.mathworks.com>...
> Hi,
> i am back again, when I posted the codes i was tired and a posted bad code, sorry for that.
> Now I think that i got it good realised in the maths form, and the achievement looks like the numbers i wanted. But i still dont know how to get graph like that :-( maybe its a bad way to do it like a matrix, but can you have a look on this code ?
>
> sigma=zeros(1249,1249);
> a=0;
> for x=-0.0624:0.0001:0.0624;
> a=a+1;
> b=0;
> for y=-0.0624:0.0001:0.0624;
> b=b+1;
> P=6E-4;
> R=0.0625;
> t=1;
> beta1=((R-y)^2+x^2);
> beta2=((R+y)^2+x^2);
> sigma(a,b)=(-2*P/(pi*t))*((x^2)*(R-y)/(beta1^2)+(x^2)*(R+y)/(beta2^2)-1/(2*R));
> end
> end
>
> i will tell you why i used such a code step by step....
> matrix is size (1249x1249) because with such a step that is used later it shoul be so size,
> x and y are a little bit smaller then R, because R is a diameter of the object, and in beta if i caltulate it with the same size it is math error later, and it has no sense to calculate it outside the circle
> a and b are indexes in the matrix, and this is what could be bad, because on the picture the axis are from -1 to 1
> beta1 and beta2 just take as they are, i didnt want you to study the whole problem, i just need help :-(
> When i run this m-file, i get a lot of good numbers, and some bad ( that could be outside the circle maybe ) and i have no idea how to draw such a nice graph like in your example. I was working on it today but nothing more comes to my mind today :-(
> I would be thankful for any other help, or ideas....
- - - - - - - - - - - -
  There is no need to use for-loops to calculate 'sigma' in the code you have shown. Just use the matlab 'meshgrid' function.

[x,y] = meshgrid( linspace(-0.0624,0.0624,1249),...
                  linspace(-0.0624,0.0624,1249) );
sigma = -2*P/(pi*t) * ( (x.^2).*(R-y)./((R-y).^2+x.^2) + ...
                        (x.^2).*(R+y)./((R+y).^2+x.^2) - 1/(2*R) );

  I don't understand why you say that "it has no sense to calculate it outside the circle". I presume you mean the circle, x^2+y^2=R^2, which is shown in black in your diagram. The above expression makes perfectly good sense outside that circle except that the two points at the top and bottom of the diagram, (0,R) and (0,-R), are singularities. All other points would have finite values.

  To generate your diagram which appears only in the interior of that circle, you would need to apply the logical condition x.^2+y.^2<=R^2 to the 'sigma' array with other points being set to whatever represents a white background.

Roger Stafford

Subject: what kind of plot ?

From: Roger Stafford

Date: 16 May, 2011 00:53:04

Message: 12 of 15

"Roger Stafford" wrote in message <iqpp8e$h19$1@newscl01ah.mathworks.com>...
> sigma = -2*P/(pi*t) * ( (x.^2).*(R-y)./((R-y).^2+x.^2) + ...
> (x.^2).*(R+y)./((R+y).^2+x.^2) - 1/(2*R) );
- - - - - - - - - -
  Oops I left out the squares on the beta expressions. It should be:

sigma = -2*P/(pi*t) * ( (x.^2).*(R-y)./((R-y).^2+x.^2).^2 + ...
                        (x.^2).*(R+y)./((R+y).^2+x.^2).^2 - 1/(2*R) );

Roger Stafford

Subject: what kind of plot ?

From: Fero

Date: 16 May, 2011 16:31:04

Message: 13 of 15

Thanks, you both helped me a lot, now one last problem i have. The calculation looks good, and the numbers are good, but, there is a very big difference between the biggest and smallest number. Is there a way, how to define the colorbar to draw colours from lets say 0.001 to -0.001 ? To define the interval of tha colorbar ? because the graph gives me a a lot of number that are out of my interval, and then few numbers that i want to see in the graph, so i need to say the colorbar, that i want to draw this specific numbers....

Subject: what kind of plot ?

From: ImageAnalyst

Date: 16 May, 2011 16:38:11

Message: 14 of 15

On May 16, 12:31 pm, "Fero " <secur...@kinanitra.sk> wrote:
> Thanks, you both helped me a lot, now one last problem i have. The calculation looks good, and the numbers are good, but, there is a very big difference between the biggest and smallest number. Is there a way, how to define the colorbar to draw colours from lets say 0.001 to -0.001 ? To define the interval of tha colorbar ? because the graph gives me a a lot of number that are out of my interval, and then few numbers that i want to see in the graph, so i need to say the colorbar, that i want to draw this specific numbers....

------------------------------
I gave you one way to adjust the colormap - you can play around with
that if you want. The other way would be to just display in the
"interesting" range and let the others be clipped. You can specify
the range of values to display in imshow(). For example
imshow(array, [5 10]);
will display all values between 5 and 10. Values less than 5 will
display as 0, values greater than 10 will display as 255, and values
in between will get mapped linearly to the 0-255 range.

Subject: what kind of plot ?

From: Roger Stafford

Date: 16 May, 2011 20:43:03

Message: 15 of 15

"Fero " <security@kinanitra.sk> wrote in message <iqmo3m$f1j$1@newscl01ah.mathworks.com>...
> sigma(a,b)=(-2*P/(pi*t))*((x^2)*(R-y)/(beta1^2)+(x^2)*(R+y)/(beta2^2)-1/(2*R));
- - - - - - - - - -
  Fero, according to the code you used in the seventh article in this thread, if x is held at a constant zero value while y ranges between -R and +R, the value of sigma should remain precisely constant at P/(pi*t*R) (except for singularities right at the endpoints.) However, this does not seem to be in accord with the color diagram I downloaded from

 http://imghost.sk/share/359423-plot.jpeg

which shows the color along the central vertical diameter changing at its upper and lower ends from blue to yellow to red - in other words through the full range of colors. Are you sure your formula is correct?

  Or perhaps that central diameter in the diagram was obtained with values off to either side of an exact zero value for x. In your case your central x value should be right at zero, so the central color should remain fixed down in the blue, shouldn't it?

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us