How do I fix this "Array indices must be positive integers or logical values error?"

1 view (last 30 days)
% Diodecurv.m
% Diode exponential curve matching and plotting
%
% Plots measured data and exponential curve fit to measured data
%
%
clear
idmeas=[0.000017 0.000104 0.0172 0.05042 0.174 0.56 1.49 4.33 13.98]; %Measured id values in amps for testing fit
vdmeas=[0.1191 0.2016 0.405 0.450 0.500 0.553 0.599 0.649 0.704]; %Measured vd values for testing fit
% Create the exponential fit to measured data
is=15*10^9;
n=2;
vt=.026;
e=2.71828;
f=fit(vdmeas',idmeas','exp1','TolFun',1e-10); %Tighter tolerance than default
vd1=[0:0.005:0.7]; %Create voltage point array for matched function values
id1=f(vd1); %Calculate matched function values
% The current values are plotted in mA by scaling x1000
idcalc=is((e.^(vd1/n.*vt))-1);
plot(vd1,idcalc)
plot(exp1)
plot(vdmeas,idmeas*1000,'o',vd1,id1*1000)
title('Diode Forward Characteristic')
xlabel('V_{D}, V')
ylabel('I_{D}, mA')
grid

Answers (1)

Steven Lord
Steven Lord on 3 Oct 2022
I have comments on a couple of lines in your code.
% Diodecurv.m
% Diode exponential curve matching and plotting
%
% Plots measured data and exponential curve fit to measured data
%
%
clear
idmeas=[0.000017 0.000104 0.0172 0.05042 0.174 0.56 1.49 4.33 13.98]; %Measured id values in amps for testing fit
vdmeas=[0.1191 0.2016 0.405 0.450 0.500 0.553 0.599 0.649 0.704]; %Measured vd values for testing fit
% Create the exponential fit to measured data
is=15*10^9;
n=2;
vt=.026;
e=2.71828;
You don't need to define this constant yourself. To compute just use the exp function.
f=fit(vdmeas',idmeas','exp1','TolFun',1e-10); %Tighter tolerance than default
vd1=[0:0.005:0.7]; %Create voltage point array for matched function values
id1=f(vd1); %Calculate matched function values
The objects returned by fit have defined the () operator to allow you to evaluate the fit at a particular value, so it's okay that vd1 contains values that are not integer values.
% The current values are plotted in mA by scaling x1000
idcalc=is((e.^(vd1/n.*vt))-1);
The variable named is, on the other hand, is not such an object. The () operator for that variable attempts to index into the variable, not evaluate. So the expression inside the parentheses does need to be a positive integer value (or an array of positive integer values.) In your case they aren't, so MATLAB gives the error message you quoted in your question.
Did you instead mean to multiply is by the value of that expression? If so add the multiplication operator (* or .*, in this particular case since the variable is contains a scalar value they behave the same) explicitly.
plot(vd1,idcalc)
plot(exp1)
Nowhere in your code do you define a variable named exp1. Is this a function?
Also, if you want to plot multiple curves on the same axes you should turn hold on first.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!