unable to plot the graph

2 vues (au cours des 30 derniers jours)
Apurva Kulkarni
Apurva Kulkarni le 6 Fév 2016
Commenté : Apurva Kulkarni le 8 Fév 2016
Hey, I am trying this code to generate a graph between voltage (x axis) and resistance(y axis), but i get an error saying the index aren't of the same length. below is the code, please advice..
close all;
clear all;
un=2000*10^-4;
cox=10*10^-3;
vtn=0.4;
wl=1;
kn=un*cox*wl;
vgs=4;
vds=0:5;
m=length(vds);
for i=1:m
if vgs>vtn
if vds(i)<vgs-vtn
rdsl(i)=1./kn*(vgs-vtn-vds(i));
elseif vds(i)>vgs-vtn
rdss(i)=0;
end
end
end
plot(vds,rdsl(i));
xlabel('voltage vds');
ylabel('resistance rds');
title('qualitative graph between voltage and resistance');

Réponse acceptée

Geoff Hayes
Geoff Hayes le 6 Fév 2016
Apurva - when I run your code, I observe the error
Index exceeds matrix dimensions.
because of the line
plot(vds,rdsl(i));
vds is an array of six elements whereas rdsl is an array of four elements of which you are tying to access the ith element. There are two problems with this - the first is the error which is telling you that you are trying to access an element outside of the dimensions of the array. i will be six yet there are only four elements in the array. I suspect that since this line of code is outside of the for loop, you don't actually need the index into rdsl. (If you had been plotting this on each iteration of the for loop, then you would need to do something like
plot(vds(i),rdsl(i));
hold on;
% etc.
Outside of the for loop, you would just need to do
plot(ids,rdsl);
The second problem is that ids is six element whereas rdsl is four. Why the discrepancy? Look closely at your elseif body
rdss(i)=0;
This seems to be a typo and should read
rdsl(i)=0;
A couple other things - MATLAB uses i and j to represent the imaginary number so it is good practice to avoid naming indexing variables as i or j. Secondly, pre-allocate your rdsl array outside of the for loop since you know that there are exactly six elements. You can do
m=length(vds);
rdsl = zeros(m,1);
This will ensure that rdsl and vds are of the same dimension. Finally, examine your elseif - is this necessary? Can you just use an else instead to capture the case where
vds(i)>=vgs-vtn
as
if vds(i)<vgs-vtn
rdsl(i)=1./kn*(vgs-vtn-vds(i));
else
rdsl(i)=0;
end
(Technically, the else isn't necessary since rdsl has been initialized to an array of all zeros.)
Try implementing the above changes and see what happens!
  1 commentaire
Apurva Kulkarni
Apurva Kulkarni le 8 Fév 2016
Thank You Geoff! Preallocating rdsl worked!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 6 Fév 2016
You assign to rdsl in one place and to rdss in another. As a result, the two arrays might not end up the same length.

Catégories

En savoir plus sur MATLAB 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