Effacer les filtres
Effacer les filtres

For loop function help

1 vue (au cours des 30 derniers jours)
Nhat Huy
Nhat Huy le 2 Déc 2023
Commenté : Nhat Huy le 3 Déc 2023
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

Réponses (1)

Walter Roberson
Walter Roberson le 2 Déc 2023
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 commentaires
Walter Roberson
Walter Roberson le 2 Déc 2023
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 le 3 Déc 2023
Thanks you guys for helping me with this problem. I'm kinda noob about this.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by