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

Risposte (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)
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 Commenti

Nhat Huy
Nhat Huy il 2 Dic 2023
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 il 3 Dic 2023
Thanks you guys for helping me with this problem. I'm kinda noob about this.

Accedi per commentare.

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Richiesto:

il 2 Dic 2023

Commentato:

il 3 Dic 2023

Community Treasure Hunt

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

Start Hunting!

Translated by