Error While running a while loop (index exceeds number of array elements)

2 ビュー (過去 30 日間)
Mia Loposser
Mia Loposser 2020 年 10 月 26 日
編集済み: Cris LaPierre 2020 年 10 月 26 日
  1 件のコメント
Mia Loposser
Mia Loposser 2020 年 10 月 26 日
clear
close
%givens
E0 = 3; %input voltage
ls = 7*10^-14; %value of ls
R = 200; %value of resistor
C = .026; %value of KT/q
V_vec = [];
Vdiode(1) = 1; %initial guess for Vdiode
Vdiode(2)= 2;
i=1;
%iterate until difference between last 2 Vdiodes < 10^-6
while abs(Vdiode(i + 1) - Vdiode(i)) > 10^-6
% derivative of f
fprime = -1-ls*R/C * exp(Vdiode(i)/C);
% f at Vdiode
f = E0 - Vdiode(i) - ls * R * (exp(Vdiode(i)/C) - 1);
% new Vdiode
Vdiode(i) = Vdiode(i) - f/fprime;
% store in vector
V_vec(i) = Vdiode(i);
% increment i
i = i + 1;
end
Vdiode
length(V_vec)

サインインしてコメントする。

回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 10 月 26 日
編集済み: Cris LaPierre 2020 年 10 月 26 日
You create variable Vdiode so that it contains a single value, 1. Then in your while condition, you try to compare the second value with the first. There is no second value, resulting in the error you see. I've transcribed the relevant parts of your code below and run it so you can see.
clear
Vdiode(1) = 1
Vdiode = 1
Vdiode(2)
Index exceeds the number of array elements (1).
  2 件のコメント
Mia Loposser
Mia Loposser 2020 年 10 月 26 日
thank you for your help! sorry about that. after adding a value for Vdiode(2) i am still recieving the error message:
index exceeds the number of array elements (2)
Cris LaPierre
Cris LaPierre 2020 年 10 月 26 日
編集済み: Cris LaPierre 2020 年 10 月 26 日
This is going to be a cyclical issue until you fix the underlying logic. Step through your code one loop at a time. There is an error around either when you increment i, or which two values you compare (right now you are including the next value. Perhaps you want to look at the previous value instead? This means (i-1) and i).

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by