Subscript indices must either be real positive integers or logicals.

조회 수: 1 (최근 30일)
Ahmed Saleem
Ahmed Saleem 2020년 12월 31일
댓글: Rik 2020년 12월 31일
Error in line 21
Plz help me with this. Whenever I sign them arrays it gives me error but without array it doesnt. Im using arrays to plot it.
clc
% Part a
% ps1=exp (16.59158-(3643.31/(t-33.424)))
% ps2=exp (14.25326-(2665.54/(t-53.424)))
% T = 318.15 K and x1 = 0.25
t=318.15;
p=zeros(1,21);
y1=zeros(1,21);
y2=zeros(1,21);
for i=1:0.05:2
syms x2 ps1 ps2 p y1 y2
x1=i-1;
x2=1-x1;
k=((i-1+0.05)/0.05);
ps1 = exp (16.59158-(3643.31/(t-33.424)));
ps2 = exp (14.25326-(2665.54/(t-53.424)));
% eqn1= p== (x1*ps1)+(x2*ps2);
p(1,k)= double(solve(p== (x1*ps1)+(x2*ps2)));
% eqn2= y1== (x1*ps1)/p;
y1(1,k)= double(solve(y1== (x1*ps1)/p(1,k)))
% eqn3= y2== (x2*ps2)/p;
y2(1,k)= double(solve(y2== (x2*ps2)/p(1,k)))
end

채택된 답변

Rik
Rik 2020년 12월 31일
Welcome to the wonderful world of floating point numbers. Just like 1/3 cannot be represented exactly with decimal values (meaning 3*(1/3) will be 0.9999), Matlab can't always store the numbers with an exact representation. This causes the rounding errors you see:
for i=1:0.05:2
k=((i-1+0.05)/0.05);
fprintf('k=%.20f\n',k)
end
k=1.00000000000000000000 k=2.00000000000000088818 k=3.00000000000000133227 k=3.99999999999999777955 k=4.99999999999999822364 k=5.99999999999999911182 k=7.00000000000000000000 k=8.00000000000000177636 k=8.99999999999999822364 k=9.99999999999999822364 k=11.00000000000000000000 k=12.00000000000000177636 k=13.00000000000000177636 k=13.99999999999999822364 k=15.00000000000000355271 k=16.00000000000000000000 k=17.00000000000000000000 k=18.00000000000000000000 k=18.99999999999999644729 k=20.00000000000000355271 k=21.00000000000000000000
You can use round to force an integer value.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by