MATLAB plotting/indexing problem

  • Context: MATLAB 
  • Thread starter Thread starter lmc2191
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The forum discussion addresses a MATLAB plotting and indexing issue encountered in the function PlotISD. The user received an error message indicating an invalid index when attempting to access the IDS1 array. The solution involved changing the index calculation from index = (VGBi+0.05)./0.05; to index = int16((VGBi+0.05)./0.05);. The discussion clarifies that the ./ operator performs element-wise division, not integer division, and suggests alternative functions such as fix or round for similar calculations.

PREREQUISITES
  • Familiarity with MATLAB programming language and syntax
  • Understanding of MATLAB array indexing and data types
  • Knowledge of element-wise operations in MATLAB
  • Basic concepts of numerical methods and plotting in MATLAB
NEXT STEPS
  • Explore MATLAB array indexing and data type conversions
  • Learn about element-wise operations in MATLAB, including ./, .*, and .^
  • Investigate MATLAB plotting functions and best practices for visualizing data
  • Study numerical methods for handling floating-point arithmetic in MATLAB
USEFUL FOR

MATLAB programmers, engineers, and researchers who are troubleshooting indexing issues or optimizing plotting functions in their MATLAB code.

lmc2191
Messages
2
Reaction score
0
Hello,

I am having a problem getting this MATLAB program to work:

function [ ] = PlotISD()
W = 10^-4;
L = 10^-5;
mu = 400;
Cox = 1.73*10^-6;
VFB = -0.75;
gamma = 0.235;
phiT = 0.0259;
IDS1 = 0:0.05:1.5;
IDS2 = IDS1;
for VGBi = 0:0.05:1.5
Ys0 = FindYs0(VGBi);
YsL = FindYsL(VGBi);
Ysm = (Ys0 + YsL)/2;
am = 1 + gamma/(2*sqrt(Ysm));
index = (VGBi+0.05)./0.05;
IDS1(index) = (W/L)*mu*Cox*(VGBi-VFB-Ysm-gamma*sqrt(Ysm))*(YsL-Ys0);
IDS2(index) = (W/L)*mu*Cox*am*phiT*(YsL-Ys0);
end
VGB0 = 0:0.05:1.5;
IDST = IDS1(1:31) + IDS2(1:31);
plot(VGB0,IDS1,VGB0,IDS2,VGB0,IDST)
endFindYs0 and FindYsL are functions that do not change the value of VGBi. I keep getting the error message:

? Attempted to access IDS1(3); index must be a positive integer or logical.

Error in ==> PlotISD at 19
IDS1(index) = (W/L)*mu*Cox*(VGB-VFB-Ysm-gamma*sqrt(Ysm))*(YsL-Ys0);

Last time I checked 3 is a positive integer. What is going on here? Any help would be appreciated.
 
Physics news on Phys.org
Nevermind, I got it.

changed

index = (VGBi+0.05)./0.05;

to

index = int16((VGBi+0.05)./0.05);

I find it strange that I had to do that. Isn't "./" integer division? Shouldn't that output an integer?
 
lmc2191 said:
Nevermind, I got it.

changed

index = (VGBi+0.05)./0.05;

to

index = int16((VGBi+0.05)./0.05);

I find it strange that I had to do that. Isn't "./" integer division? Shouldn't that output an integer?

No, "./" means term by term division as opposed to matrix (inverse multiplication) division. Same for ".*" versus "*" and ".^" versus "^".

BTW. You could also have used either "fix((VGBi+0.05)./0.05)" or "round((VGBi+0.05)./0.05)"
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
27K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
5
Views
31K
  • · Replies 2 ·
Replies
2
Views
3K