Hello everyone,
I have created a struct S as:
syms x1 x2 nnX1 nnX2
p1Space = linspace(0,50,10);
p2Space = linspace(0,25,10);
for i=1:10
S(i) = vpasolve(x1^2 + x2^2 - p1Space(i) == 0, x1*x2 - p2Space(i) == 0, [x1 x2]);
end
and I get an error when I perfom the following for loop in order to store specific elements into two arrays (XX1 and XX2):
for k = 1:10
XX1(k) = S(k).x1(2,1);
XX2(k) = S(k).x2(2,l);
end
When I type in the command window for example S(4).x1(2,1), the number appears on the screen, but inside the for-loop, this doesn't work. Any ideas why is this happening and how can I store these spesific values into the XX1 and XX2 arrays?
Thank you

댓글 수: 1

Ameer Hamza
Ameer Hamza 2020년 3월 18일
You are getting this error because S(1).x1 does not have two elements. Similar is the case for S(1).x2. All the other elements of S have two elements, therefore S(4).x1(2,1) in the command window. Try S(1).x1(2,1) in the command window.

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

 채택된 답변

Adam Danz
Adam Danz 2020년 3월 18일
편집: Adam Danz 2020년 3월 18일

1 개 추천

As stated in the documentation, the size of the vpasolve output corresponds to the number of the solutions. On the first iteration, there is only 1 solution which can be seen here,
S(1)
ans =
struct with fields:
x1: [1×1 sym]
x2: [1×1 sym]
On the first iteration of the k-loop (k=1), you're getting the error because you're trying to get the 2nd row of x1 and x2 but they only have 1 row.

추가 답변 (1개)

Konstantinos Andrikos
Konstantinos Andrikos 2020년 3월 18일

0 개 추천

Thank you for your responses. Problem solved!

댓글 수: 1

Adam Danz
Adam Danz 2020년 3월 18일
편집: Adam Danz 2020년 3월 18일
Glad I could help.

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

카테고리

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

태그

질문:

2020년 3월 18일

편집:

2020년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by