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

1 view (last 30 days)
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);

Answers (2)

Matt J
Matt J on 9 Nov 2022
Edited: Matt J on 9 Nov 2022
Correct the missing space in,
fori=1:n;

Kalpesh Bagmar
Kalpesh Bagmar on 9 Nov 2022
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

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!