matlab - almost done! simple mistake in function graphing ?

4 vues (au cours des 30 derniers jours)
Chris
Chris le 3 Sep 2011
Hi ,
i am trying to graph kx, ky, K (constant line for k) on the y axis, and the Length (L) from 30 to 140 on the xaxis, as of right now I get the constant line from 30 to 140, but the other values for kx and ky start from zero. I need the values for kx and ky to go from 30 to 140 as well.
Any ideas?
clear
clc
mfp = 31; % mean free path (nm)
k = 316.72; % conductivity (W / m*k)
% Equations
Lmax = 140;
dL=10;
L=30;
while L<=Lmax
kx(L)=k*(1-((2*mfp)/(3*3.14*L)))
ky(L)=k*(1-(mfp/(3*L)));
L= L+dL;
end
kx(~kx)=[]; % removes all zeros from kx vector
ky(~ky)=[]; % removes all zeros from ky vector
plot(kx,'-'),hold on, plot(ky,'--'), hold on, plot([30,140],[316.72,316.72]);
grid on;
%plot(kx,'-'),hold on, plot(ky,'--'), hold on, plot(k);
title(['Conductivity (W / m*k ) ']);
xlabel('Thickness (nm)','FontSize',14);
ylabel('Conductivity','FontSize',14);
leg1=legend('kx','ky');
this was given to me for the last time i asked, and it makes the graph curve like it should. it just needs to be from 30 to 140 isntead of 0 to 11 or which one it goes too.
kx(~kx)=[]; % removes all zeros from kx vector
ky(~ky)=[];

Réponses (1)

Oleg Komarov
Oleg Komarov le 3 Sep 2011
If you remove the zeros then your kx and ky will have less values and if you omit the x-values when calling plot by default it sets them to 1:numel(y-values). Therefore, say you have 12 values after removing the zeros it will plot kx from 1 to 12.
What you have to do is find at which positions your non-zero values are and call plot(X,Y).
Another remark, since you know how many iterations you'll have to do in the loop, call explicitly for.
mfp = 31; % mean free path (nm)
k = 316.72; % conductivity (W / m*k)
% Preallocate kx and ky (search for preallocation for details)
kx = zeros(Lmax,1);
ky = zeros(Lmax,1);
% Loop from 30 to 140 with a step of 10.
for ii = 30:10:140
kx(ii) = k*(1-(2*mfp/(3*3.14*ii)));
ky(ii) = k*(1-(mfp/(3*ii)));
end
% Find the positions (x coordinates) of the non zero entries in kx
idx = find(kx);
plot(idx,kx(idx),'-')
hold on
% Find the positions (x coordinates) of the non zero entries in ky
idx = find(ky);
plot(idx,ky(idx),'--')
plot([30,140],[316.72,316.72]);
grid on
EDIT
A vectorized alternative:
mfp = 31; % mean free path (nm)
k = 316.72; % conductivity (W / m*k)
L = 30:10:140;
kx = k*(1-(2*mfp./(3*3.14*L)));
ky = k*(1-(mfp./(3*L)));
x = 1:numel(L);
plot(x,kx,'-',x,ky,'--',[x(1) x(end)],[316.72,316.72])
set(gca,'Xlim',[x(1) x(end)],'XtickLabel',L)
  5 commentaires
Oleg Komarov
Oleg Komarov le 3 Sep 2011
You can keep your while loop but that's not the core of your problem.
There are tons of solutions and I presented you with an easy one. Find returns the positions of all non zero elements (for more details read the documentation). This way you keep the reference to the [0 140].
Walter Roberson
Walter Roberson le 3 Sep 2011
Your while loop already goes from 30 to 140. It is the graph that is the difficulty. Oleg's code should make the graph reflect 30 to 140.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by