dsolve generating inconsistent outputs

88 Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

The problem might be that you're passing strings to dsolve rather than symbolic expressions. This means that in the first case rate_i might be interpreted as a constant, rather than a function of t.

Here's what you're probably trying to do: defining Dheight as a sym as well, and telling dsolve what to do using syms:

%Code1:

clear Qua t Area height Dheight
syms Qua t Area height(t) Dheight

Dheight = diff(height);
rate_in = 3*Qua*(sin(t))^2; 
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)

%Code2:

clear Qua t Area height Dheight
syms Qua t Area height(t) Dheight

Dheight = diff(height);
rate_in = 3*Qua*(sin(t))^2; 
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)

Changes to your version:

  • I removed delta_Vol since it wasn't used and contained an unclear reference to (D)AreaY
  • I changed in dsolve from string to symbolic expression, in the mean time = had to be changed to ==
  • I defined DHeight as diff(Height), which implies that height has to be declared as height(t). This also allows us to define the initial condition as height(0)==0, otherwise we'd have needed to keep this a string: 'height(0)=0'.

Now both versions return the same solution:

fnc_main = 

    @(t)t.*(9.0./5.0e1)-sin(t.*2.0).*(2.7e1./1.0e2)

I suggest checking on paper whether this solution, or its symbolic predecessor,

delta_Height =

(Qua*(2*t - 3*sin(2*t)))/(4*Area)

is indeed a solution to your differential equation.