필터 지우기
필터 지우기

For loop function help

조회 수: 1 (최근 30일)
Nhat Huy
Nhat Huy 2023년 12월 2일
댓글: Nhat Huy 2023년 12월 3일
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일
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
Walter Roberson
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
Nhat Huy 2023년 12월 3일
Thanks you guys for helping me with this problem. I'm kinda noob about this.

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

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by