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 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