the variable appears to change size every loop iteration

5 ビュー (過去 30 日間)
Nisreen Aljumaili
Nisreen Aljumaili 2021 年 9 月 30 日
回答済み: Image Analyst 2021 年 9 月 30 日
%(a)Write the following script to plot the magnetization curve.
clc
clear
If = [0 0.5 1.0 1.5 2.0 2.5];
Ea = [0 75 150 205 242 270];
Ra = 0.14;
RI = 2;
la = 100;
Ifield = linspace(0,2.5,100);
for n = 1:100
Ea_curve(n) = spline(If,Ea,lfield(n));
end
plot(Ifield, Ea_curve)
title ('Field current vs. Generated EMF')
xlabel('Field current [A]')
ylabel('Generated EMF [V]')

回答 (2 件)

Alan Stevens
Alan Stevens 2021 年 9 月 30 日
You don't need the loop:
If = [0 0.5 1.0 1.5 2.0 2.5];
Ea = [0 75 150 205 242 270];
Ra = 0.14;
RI = 2;
la = 100;
Ifield = linspace(0,2.5,100);
Ea_curve = spline(If,Ea,Ifield);
plot(Ifield, Ea_curve)
title ('Field current vs. Generated EMF')
xlabel('Field current [A]')
ylabel('Generated EMF [V]')
  2 件のコメント
Nisreen Aljumaili
Nisreen Aljumaili 2021 年 9 月 30 日
thank you so much:)
I am sorry i tried the same way with this code and did not use the loop but it did not work
clc
clear
If = [0 0.5 1.0 1.5 2.0 2.5];
Ea = [0 75 150 205 242 270];
Ra = 0.14
RI = 2;
Ifield = linspace(0,2.5,100);
for n = 1:100
Ea_curve(n) = spline(lf, Ea, Ifield(n));
la(n) = Ea_curve(n)/2;
Vt(n) = Ea_curve(n)-la(n)*Ra;
Pload(n) = Ea_curve(n)*la(n);
end
plot(Ifield, Vt)
title ('Field current vs. Terminal voltage')
xlabel('Field current [A]')
ylabel('Terminal voltage [V]')
Alan Stevens
Alan Stevens 2021 年 9 月 30 日
If = [0 0.5 1.0 1.5 2.0 2.5];
Ea = [0 75 150 205 242 270];
Ra = 0.14;
RI = 2;
Ifield = linspace(0,2.5,100);
Ea_curve = spline(If, Ea, Ifield); % If not lf
la = Ea_curve/2;
Vt = Ea_curve-la*Ra;
Pload = Ea_curve.*la; % Notice the dot in .*
plot(Ifield, Vt)
title ('Field current vs. Terminal voltage')
xlabel('Field current [A]')
ylabel('Terminal voltage [V]')

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


Image Analyst
Image Analyst 2021 年 9 月 30 日
You're getting the warning because of this line
Ea_curve(n) = spline(If,Ea,lfield(n));
So every time you want to stuff a new number into the nth location of Ea_curve, it has to reallocation a new chunk of memory as big as the entire array, not just one 8 byte chunk for the additional element. To avoid the error you can preallocate Ea_curve with zeros before the loop
Ea_curve(n) = zeros(1, 100);
for n = 1 : 100

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by