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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by