Array indices must be positive integers or logical values. in line 5

1 回表示 (過去 30 日間)
Saurabh Kore
Saurabh Kore 2022 年 11 月 9 日
回答済み: Kalpesh Bagmar 2022 年 11 月 9 日
n=input('\n Enter number of elements = ');
fori=1:n;
tx=input('\n Enter the value of x = ');
temp=input('\n Enter the value of y = ');
x(i)=log(tx);
y(i)=log(temp);
s0=0;
s1=0;
s2=0;
s3=0;
for i=1:n
s0=s0+x(i);
s1=s1+y(i);
s2=s2+x(i)*y(i);
s3=s3+x(i)*x(i);
end
d=s0*s0-n*52;
da=s1*s0n*52;
db=s0*s2-s1*s3;
a1=da/d;
b1=db/d;
b=a1;
a=exp(b1);
fprintf('\n y=(%f)x^(%f)',a,b);

回答 (2 件)

Matt J
Matt J 2022 年 11 月 9 日
編集済み: Matt J 2022 年 11 月 9 日
Correct the missing space in,
fori=1:n;

Kalpesh Bagmar
Kalpesh Bagmar 2022 年 11 月 9 日
I dont know what exactly are you trying to achieve with this logic. If you can throw some light on that, it will be helpful.
I have edited your code snippet to make it error free, if in case this is want you want.
n=input('\n Enter number of elements = ');
x={}
y = {}
for i=1:n
tx=input('\n Enter the value of x = ');
temp=input('\n Enter the value of y = ');
x{i}=log(tx);
y{i}=log(temp);
s0=0;
s1=0;
s2=0;
s3=0;
for j=1:i %we cannot iterate to n as x array only has i elements at any given time
s0=s0+x{j};
s1=s1+y{j};
s2=s2+x{j}*y{j};
s3=s3+x{j}*x{j};
end
d=s0*s0-n*52;
da=s1*s0-n*52;
db=s0*s2-s1*s3;
a1=da/d;
b1=db/d;
b=a1;
a=exp(b1);
fprintf('\n y=(%f)x^(%f)',a,b);
end

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by