generation of data plot using an equation

4 visualizaciones (últimos 30 días)
Hirak
Hirak el 18 de Dic. de 2013
Comentada: Niklas Nylén el 19 de Dic. de 2013
Hi Friends, I am a newbie in this world. Major of my work consists of generation of data plot. Recently I am need for a code in which I can vary r_shell for a fixed value of r_core to find out g from the given equation:
  • g=[(r_core+r_shell)^3-r_shell^3]/(r_core+r_shell)^3
  • The range of r_shell is 0 to 5.2 with gap 0.26. In each case, the value of r_core is fixed at 5. Plot should be r_shell in x-axis and g in y-axis.I also need the data in tabulated form.My code was:
if true
clear all;
close all;
dbstop if error;
r_shell=vertcat(0:.26:5.2);
r_core=5;
for i=0:length(r_shell);
a = (r_core+i).^ (3)-(i).^ (3);
g= a/(r_core+i).^3;
end
temp=[i;g]';
save('temp','-ascii')
figure
plot(temp(:,1),temp(:,2));
xlabel('Shell thickness(nm)')
ylabel('Murray g factor');
save('diel.tab','temp','-ascii') % code end
I wrote the code but due to my lack of knowledge it remain failed to made it. Please suggest me. I am in deep need of it. Thank you all and Merry Christmas for you and family. Hirak

Respuesta aceptada

Hirak
Hirak el 18 de Dic. de 2013
Thank you Niklas. It worked nicely. Now, if I want to vary r_core also, say, for the spread 1:0.1:10, and make a three dimensional plot,what should I do? I want to learn MATLAB. From where should I start? There is no one here to teach me MATLAB. Please suggest me. With warm regards, Hirak.

Más respuestas (1)

Niklas Nylén
Niklas Nylén el 18 de Dic. de 2013
I can see a few issues in your code:
The use of vertcat when defining r_shell seems unnecessary, this should give the same result:
r_shell=0:.26:5.2;
Matlab indexes start at 1, not 0, and you also use the counting variable i instead of the value r_shell(i), but in this case a for loop is unnecessary. A matrix operation is faster:
g=((r_core+r_shell).^3-r_shell.^3)./(r_core+r_shell).^3
To plot the data:
figure
plot(r_shell, g);
xlabel('Shell thickness(nm)')
ylabel('Murray g factor');
And save the data in an ascii file
data = [r_shell; g]:
save('diel.tab','tmp','-ascii')
  2 comentarios
Hirak
Hirak el 19 de Dic. de 2013
Thank you Niklas. It worked nicely. Now, if I want to vary r_core also, say, for the spread 1:0.1:10, and make a three dimensional plot,what should I do? I want to learn MATLAB. From where should I start? There is no one here to teach me MATLAB. Please suggest me. With warm regards, Hirak.
Niklas Nylén
Niklas Nylén el 19 de Dic. de 2013
What you will want to do is to create two vectors where all combinations of the two original vectors are present. I will use variable names x and y in this example with some limited amount of data for readability.
Step 1: Create the original vectors (this is r_shell and r_core in your case)
>> x = 1:0.5:2;
>> y = 8:1:10;
Step 2: Create all combinations of the two vectors x and y by using the function meshgrid
>> [x1,y1] = meshgrid(x,y)
This will create two matrices where all combinations are available
x1 = 1 1.5 2
1 1.5 2
1 1.5 2
y1 = 8 8 8
9 9 9
10 10 10
Although you can use these matrices as direct inputs to your calculation, for future plotting it would be nice to have them as vectors instead.
>> x1 = x1(:)
>> y1 = y1(:)
x1 = 1 1 1 1.5 1.5 1.5 2 2 2
y1 = 8 9 10 8 9 10 8 9 10
As you can see, if you combine the elements with the same index in the two vectors, you get all possible combinations. Now you can use the vectors in a calculation, for example
>> z = (x1 + y1.^x1)./x1
z = 9.00 10.00 11.00 16.08 19.00 22.08 33.00 41.50 51.00
To plot the 3d data, use the command plot3
>> figure
>> plot3(x1,y1,z,'.')
>> grid on
It looks like the answer you accepted above was your own response, please click accept on my answer instead.
As for learning matlab, I would suggest either searching online for some small projects that you can create. There are also a number of beginner tutorials available.

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by