Why is my variable being changed without assignment
Hello,
I have searched quite a lot on Google and cannot find anything related to what is happening, and just in general programming terms I cannot understand it either. I’m stumped and hope someone can shed some light. An analogy of what seems to be happening is this:
A = 1
B = A + 2 = 3 (and now A = 3!!!)
This makes no sense to me, how can the original value of A be getting changed without. This is my first time using Live Script and symbols, so perhaps there is something different around those concepts.
My code is below. The issue is that when I substitute 0 into yh(zero), it is also changing the value of yh. Thus when I perform a subs to calc yhNat (last line), the value it is referencing is no longer yh but actually yh(zero). If I leave out the calc of yh(zero) then yhNat calculates perfectly (15*exp((-2*t)) + 16*exp((-3*t))). The only thing I can think of is because the initial value assigned to both yh and yh(zero) is the same, perhaps Matlab is conflating/linking them somehow. Originally I just made yh(zero) a subs of yh, replacing all t’s with 0, but that didn’t work, hence the workaround.
Any help would be greatly appreciated, as I am fully stuck and all my years of messing around with programming languages aren’t helping at all to understand why this is happening.
% Homogenous solution
InitialVoltage = 1.5;
InitialCurrent = 2;
c_UD = 1/6;
%from earlier code roots.1 = -3, roots.2 = -2
syms yh yh(zero) yhNat dyh(t) dyh(zero) c_1 c_2
yh = c_1*exp(roots(1)*t) + c_2*exp(roots(2)*t)
dyh(t) = diff(yh)
dyh(zero) = subs(dyh(t),[t],[0]) == InitialCurrent/c_UD
%Evaluate yh(t) at t = 0:
yh(zero) = c_1*exp(roots(1)*t) + c_2*exp(roots(2)*t); %######### These two lines break it
yh(zero) = subs(yh(zero), [t], [0]) == vpa(InitialVoltage) %######### These two lines break it
%coefficients = solve([dyh(zero), yh(zero)], [c_1, c_2])
%Natural response
yhNat = subs(yh ,[c_1, c_2], [16, 15])Hello,
I have searched quite a lot on Google and cannot find anything related to what is happening, and just in general programming terms I cannot understand it either. I’m stumped and hope someone can shed some light. An analogy of what seems to be happening is this:
A = 1
B = A + 2 = 3 (and now A = 3!!!)
This makes no sense to me, how can the original value of A be getting changed without. This is my first time using Live Script and symbols, so perhaps there is something different around those concepts.
My code is below. The issue is that when I substitute 0 into yh(zero), it is also changing the value of yh. Thus when I perform a subs to calc yhNat (last line), the value it is referencing is no longer yh but actually yh(zero). If I leave out the calc of yh(zero) then yhNat calculates perfectly (15*exp((-2*t)) + 16*exp((-3*t))). The only thing I can think of is because the initial value assigned to both yh and yh(zero) is the same, perhaps Matlab is conflating/linking them somehow. Originally I just made yh(zero) a subs of yh, replacing all t’s with 0, but that didn’t work, hence the workaround.
Any help would be greatly appreciated, as I am fully stuck and all my years of messing around with programming languages aren’t helping at all to understand why this is happening.
% Homogenous solution
InitialVoltage = 1.5;
InitialCurrent = 2;
c_UD = 1/6;
%from earlier code roots.1 = -3, roots.2 = -2
syms yh yh(zero) yhNat dyh(t) dyh(zero) c_1 c_2
yh = c_1*exp(roots(1)*t) + c_2*exp(roots(2)*t)
dyh(t) = diff(yh)
dyh(zero) = subs(dyh(t),[t],[0]) == InitialCurrent/c_UD
%Evaluate yh(t) at t = 0:
yh(zero) = c_1*exp(roots(1)*t) + c_2*exp(roots(2)*t); %######### These two lines break it
yh(zero) = subs(yh(zero), [t], [0]) == vpa(InitialVoltage) %######### These two lines break it
%coefficients = solve([dyh(zero), yh(zero)], [c_1, c_2])
%Natural response
yhNat = subs(yh ,[c_1, c_2], [16, 15]) Hello,
I have searched quite a lot on Google and cannot find anything related to what is happening, and just in general programming terms I cannot understand it either. I’m stumped and hope someone can shed some light. An analogy of what seems to be happening is this:
A = 1
B = A + 2 = 3 (and now A = 3!!!)
This makes no sense to me, how can the original value of A be getting changed without. This is my first time using Live Script and symbols, so perhaps there is something different around those concepts.
My code is below. The issue is that when I substitute 0 into yh(zero), it is also changing the value of yh. Thus when I perform a subs to calc yhNat (last line), the value it is referencing is no longer yh but actually yh(zero). If I leave out the calc of yh(zero) then yhNat calculates perfectly (15*exp((-2*t)) + 16*exp((-3*t))). The only thing I can think of is because the initial value assigned to both yh and yh(zero) is the same, perhaps Matlab is conflating/linking them somehow. Originally I just made yh(zero) a subs of yh, replacing all t’s with 0, but that didn’t work, hence the workaround.
Any help would be greatly appreciated, as I am fully stuck and all my years of messing around with programming languages aren’t helping at all to understand why this is happening.
% Homogenous solution
InitialVoltage = 1.5;
InitialCurrent = 2;
c_UD = 1/6;
%from earlier code roots.1 = -3, roots.2 = -2
syms yh yh(zero) yhNat dyh(t) dyh(zero) c_1 c_2
yh = c_1*exp(roots(1)*t) + c_2*exp(roots(2)*t)
dyh(t) = diff(yh)
dyh(zero) = subs(dyh(t),[t],[0]) == InitialCurrent/c_UD
%Evaluate yh(t) at t = 0:
yh(zero) = c_1*exp(roots(1)*t) + c_2*exp(roots(2)*t); %######### These two lines break it
yh(zero) = subs(yh(zero), [t], [0]) == vpa(InitialVoltage) %######### These two lines break it
%coefficients = solve([dyh(zero), yh(zero)], [c_1, c_2])
%Natural response
yhNat = subs(yh ,[c_1, c_2], [16, 15]) live script, variable assignment MATLAB Answers — New Questions