I am writing a code to solve a problem by least square method. Can someone tell what is wrong in this code?
syms x k
x(1)=-2;
x(2)=-1;
x(3)=0;
x(4)=1;
x(5)=2;
f(1)=-2;
f(2)=-1;
f(3)=0;
f(4)=1;
f(5)=2;
for i=0:4
a(i)=symsum((x(k))^i,k,1,5)
end
Error using sym/subsindex (line 857)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
Error in sym/subsref (line 902)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in NMa2q2 (line 13)
a(i)=symsum((x(k))^i,k,1,5)

답변 (1개)

Setsuna Yuuki.
Setsuna Yuuki. 2020년 11월 28일
편집: Setsuna Yuuki. 2020년 11월 28일

0 개 추천

syms x k
x(1)=-2;
x(2)=-1;
x(3)=0;
x(4)=1;
x(5)=2;
f(1)=-2;
f(2)=-1;
f(3)=0;
f(4)=1;
f(5)=2;
for i=0:4
a{i+1}=symsum((x*(k)).^i,k,1,5) %change this line
end
if you need double variables and not syms, use:
a{i+1}=double(symsum((x*(k)).^i,k,1,5))

댓글 수: 4

Ipshita
Ipshita 2020년 11월 28일
After changing the line you specified, I am still not getting the answer. It is not able to take the values of x(1), x(2)... For a(1), I should get 0 for the given problem, but using your code, I am getting this:
a(1)
ans =
1×1 cell array
{1×5 sym}
Setsuna Yuuki.
Setsuna Yuuki. 2020년 11월 28일
But the symsum() function in your code returns 5 values, for each i value.
Ipshita
Ipshita 2020년 11월 28일
yes, it should return 5 values, i.e. a(1), a(2), a(3), a(4), a(5). But it is not returning, that is the problem.
did you try using double ()?
a{i+1}=double(symsum((x*(k)).^i,k,1,5))
with double() this is the output
maybe i don't understand good your problem, sorry

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

질문:

2020년 11월 28일

댓글:

2020년 11월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by