For loop function help
이전 댓글 표시
I have a piece of code down here to solve a quadratic equation but i don't know why it fail to sovle it. I'm newbie to matlab.
syms t x;
x_t = (t.^2)/(t-1);
y_t = (t)./(t.^2 - 1);
t_middle = solve(x_t == x, t);
y = subs(y_t, t, t_middle);
[tu, mau] = numden(y);
for k = 1:length(mau)
a = solve(mau(k,1));
end
답변 (1개)
You are not displaying any results, and you are overwriting all of a each iteration.
syms t x;
x_t = (t.^2)/(t-1);
y_t = (t)./(t.^2 - 1);
t_middle = solve(x_t == x, t)
y = subs(y_t, t, t_middle)
[tu, mau] = numden(y)
for k = 1:length(mau)
a = solve(mau(k,1))
end
What this is telling you is that there are two solutions for x_t, and that one of them has a pole (discontinuity) at x = -1/2 but the other has no discontinuities.
댓글 수: 4
Nhat Huy
2023년 12월 2일
It shows something which happens to be empty, and that is the output for the last iteration of the loop (as you can see in Walter's answer above).
Here, solve() has returned an empty solution without a warning, which means no solutions exist for that particular equation.
You can save the outputs in a cell array -
syms t x;
x_t = (t.^2)/(t-1);
y_t = (t)./(t.^2 - 1);
t_middle = solve(x_t == x, t);
y = subs(y_t, t, t_middle);
[tu, mau] = numden(y)
num = length(mau);
a = cell(num,1);
for k = 1:num
a{k} = solve(mau(k,1));
end
a
Walter Roberson
2023년 12월 2일
Suppose you had the code
A = 3;
A = 5;
After the execution of that, would you expect A to hold both 3 and 5 ? No -- because the second assignment is overwriting all of the variable.
Now suppose you had the code
for K = 1 : 2
A = 2*K + 1;
end
The first iteration would have A = 2*1+1 which would be A = 3. The second iteration would have A = 2*2+1 which would be A = 5 -- the same as if you had had the code
K = 1;
A = 3;
K = 2;
A = 5;
And yet somehow you expect that because you used a loop, that afterwards A will store both 3 and 5.
When you overwrite all of a variable inside a loop, then all of the variable gets overwritten.
for K = 1 : 2
A = 2*K + 1;
end
does not mean
for K = 1 : 2
A indexed by the relative position of the current K in the list of values, is to be assigned 2*K+1
end
If you want to collect the outputs during a for loop, you have to explicitly code for the collection.
Nhat Huy
2023년 12월 3일
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






