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

Walter Roberson
Walter Roberson 2023년 12월 2일

0 개 추천

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)
t_middle = 
y = subs(y_t, t, t_middle)
y = 
[tu, mau] = numden(y)
tu = 
mau = 
for k = 1:length(mau)
a = solve(mau(k,1))
end
a = 
a = Empty sym: 0-by-1
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
Nhat Huy 2023년 12월 2일
yes, but as I disp(a) it shown nothing. I'm confuse about the loop.
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)
tu = 
mau = 
num = length(mau);
a = cell(num,1);
for k = 1:num
a{k} = solve(mau(k,1));
end
a
a = 2×1 cell array
{[-1/2 ]} {0×1 sym}
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
Nhat Huy 2023년 12월 3일
Thanks you guys for helping me with this problem. I'm kinda noob about this.

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

카테고리

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

질문:

2023년 12월 2일

댓글:

2023년 12월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by