I've been playing with this for awhile, Can anyone explain why I get different answers from Code1 and Code2? What is it about the actual script of 'dsolve()' that makes the output of the 2 codes different? Could the output be the same if I just used different syntax (ie ',;".')?
%Code1:
syms Qua t Area height
rate_in = 3*Qua*(sin(t))^2;
delta_Vol = dsolve('DAreaY = rate_in - Qua');
delta_Height= dsolve('Dheight = ((rate_in - Qua)/Area)', 'height(0) = 0');
subfnc1 = subs(rate_in, {Qua}, {450});
fnc1 = subs(delta_Height, {'rate_in'}, {subfnc1});
fnc1 = subs(fnc1, {Area, Qua}, {1250,450});
fnc_main = matlabFunction(fnc1);
fnc_main(0:10)';
%Code2:
syms Qua t Area height
rate_in = 3*Qua*(sin(t))^2;
delta_Vol = dsolve('DAreaY = 3*Qua*(sin(t))^2 - Qua');
delta_Height= dsolve('Dheight = ((3*Qua*(sin(t))^2 - Qua)/Area)', 'height(0) = 0');
fnc1 = subs(delta_Height, {Area, Qua}, {1250,450});
fnc_main = matlabFunction(fnc1);
fnc_main(0:10)';
what is it about the dsolved function that I don't understand?
The problem might be that you're passing strings to
dsolve
rather than symbolic expressions. This means that in the first caserate_i
might be interpreted as a constant, rather than a function oft
.Here's what you're probably trying to do: defining
Dheight
as asym
as well, and tellingdsolve
what to do usingsym
s:Changes to your version:
delta_Vol
since it wasn't used and contained an unclear reference to(D)AreaY
dsolve
from string to symbolic expression, in the mean time=
had to be changed to==
DHeight
asdiff(Height)
, which implies thatheight
has to be declared asheight(t)
. This also allows us to define the initial condition asheight(0)==0
, otherwise we'd have needed to keep this a string:'height(0)=0'
.Now both versions return the same solution:
I suggest checking on paper whether this solution, or its symbolic predecessor,
is indeed a solution to your differential equation.