how to use derivative of function using gradient?
Mostrar comentarios más antiguos
hi, im a student trying to solve a mathematical problem using MATLAB, the code consists ODE's (ordinary differential equations). the code is using a numerical analysis in order to solve all the ODE's simoultenously. One of the ODE's using a derivative of a parameter called "par". the parameter's value is changing along z axis (the problem defined only for z axis). this is the line:
dydz(1) = -(gradient(par,z))*(y(1)/par);
unfortenately when i display all the values of "par" using: disp(par); i get a lot of different numbers as expected. but when i use disp(gradient(par,z)); i get only zero's 0.
what am i doing wrong? is gradient function gets the gradient of a single point each time and returns zero? how do i fix this?
Thanks !
1 comentario
Gal Shaked
el 21 de Sept. de 2022
Respuestas (2)
Add the variable "dens_mix" as an algebraic equation to your system (e.g. as y(13)):
dydz(13) = y(13) - (partial_dens(1)+partial_dens(2)+partial_dens(3)+partial_dens(4));
and supply the mass matrix of your ODE system as
function M = Mass(t,y)
M = eye(13);
M(13,13) = 0;
M(1,13) = y(1)/y(13);
end
If follows that in the vector dydz that you prescribe in ODEBVP, you have to set
dydz(1) = 0
If you don't use an ODE integrator (like ODE45 or ODE15S), but a BVP solver (like BVP4C), come back here. In this case, a different solution will be necessary.
15 comentarios
Gal Shaked
el 21 de Sept. de 2022
Torsten
el 21 de Sept. de 2022
First define dy(2)/dz,...,dy(12)/dz as you did in your code.
Then you will have to differentiate using pencil and paper the identity
dens_mix = (partial_dens(1)+partial_dens(2)+partial_dens(3)+partial_dens(4));
with respect to z (and thus express d(dens_mix)/dz in terms of the z-derivatives of the variables from y(1),...,y(12) that are involved).
If d(dens_mix)/dz comes out to be some function f(dy(1)/dz,dy(2)/dz,...,dy(12)/dz), you can insert this expression in the first ODE:
dy(1)/dz = -f(dy(1)/dz,dy(2)/dz,...,dy(12)/dz)*y1/dens_mix
Since possibly both the left and the right-hand side contain dy1/dz, it might be necessary to solve for it.
Gal Shaked
el 25 de Sept. de 2022
Editada: Gal Shaked
el 25 de Sept. de 2022
Torsten
el 25 de Sept. de 2022
What comes out if you express d(dens_mix)/dz using y(1),...,y(12),dydz(1),...,dydz(12) ?
Gal Shaked
el 25 de Sept. de 2022
Torsten
el 25 de Sept. de 2022
What is equation 13 ? I do not yet know your new code.
Gal Shaked
el 25 de Sept. de 2022
Gal Shaked
el 25 de Sept. de 2022
Express density_mix in terms of y(1),..,y(12) and constants/parameters.
partial_press depends on y(10) and molar_fractions where (I guess) molar_fractions also depends on y(1),...y(12).
Partial_dens depends on y(11).
Thus dydz(13) should depend at least on y(10), y(11), dydz(10),dydz(11) and molar_fractions from which I don't know how they are computed from y(1),...,y(12).
Is it not possible for you to write down dens_mix as a function of y(1),...,y(12) and to differentiate this equation with respect to z ?
If you have difficulties, make it symbolically with MATLAB.
Gal Shaked
el 25 de Sept. de 2022
Gal Shaked
el 25 de Sept. de 2022
The use of the symbolic toolbox is only meant to differentiate the expression for dens_mix with respect to z. Once you have this derivative, you can copy it in your existing code.
But you write above
dydz(13) = something
So you already differentiated y(13) = dens_mix with respect to z and the result is the right-hand side ?
I think you wrote y(13) and not dydz(13), didn't you ?
Gal Shaked
el 26 de Sept. de 2022
Editada: Gal Shaked
el 26 de Sept. de 2022
Forget about y(13) and use
dydz(2) = y(3); %Specific mass balance (CH4)
dydz(3) = ((y(1)*y(3))+(dens_cat_weighted*r_CH4))/(D_m+D_l); %Specific mass balance (CH4)
dydz(4) = y(5); %Specific mass balance(H2O)
dydz(5) = ((y(1)*y(5))+(dens_cat_weighted*r_H2O))/(D_m+D_l); %Specific mass balance(H2O)
dydz(6) = y(7); %Specific mass balance(CO2)
dydz(7) = ((y(1)*y(7))+(dens_cat_weighted*r_CO2))/(D_m+D_l); %Specific mass balance(CO2)
dydz(8) = y(9); %Specific mass balance (H2)
dydz(9) = ((y(1)*y(9))+(dens_cat_weighted*r_H2))/(D_m+D_l); %Specific mass balance(H2)
dydz(10) = -(1/1000)*(((dyn_visc_mix/K_DPC)*y(1))+((dens_mix/K_nDC)*(y(1)^2))); % Momentum balance
dydz(11) = y(12); %energy balance (temprature)
dydz(12) = ((enthalpy*r*dens_cat_weighted*1000) - (flux/H) + (specific_heat*dens_mix*y(1)*y(12)))/(k_m); %energy balance
d_dens_mix_dz = (dydz(10)*y(11)-y(10)*dydz(11))/(Rconstant*y(11)^2)*(...
molecular_weights(1)*y(2)/(y(2)+y(4)+y(6)+y(8))+...
molecular_weights(2)*y(4)/(y(2)+y(4)+y(6)+y(8))+...
molecular_weights(3)*y(6)/(y(2)+y(4)+y(6)+y(8))+...
molecular_weights(4)*y(8)/(y(2)+y(4)+y(6)+y(8)))+...
(y(10)/(Rconstant*y(11))*(...
molecular_weights(1)*((dydz(2)*(y(2)+y(4)+y(6)+y(8))-...
y(2)*(dydz(2)+dydz(4)+dydz(6)+dydz(8)))/...
(y(2)+y(4)+y(6)+y(8))^2)+...
molecular_weights(2)*((dydz(4)*(y(2)+y(4)+y(6)+y(8))-...
y(4)*(dydz(2)+dydz(4)+dydz(6)+dydz(8)))/...
(y(2)+y(4)+y(6)+y(8))^2)+...
molecular_weights(3)*((dydz(6)*(y(2)+y(4)+y(6)+y(8))-...
y(6)*(dydz(2)+dydz(4)+dydz(6)+dydz(8)))/...
(y(2)+y(4)+y(6)+y(8))^2)+...
molecular_weights(4)*((dydz(8)*(y(2)+y(4)+y(6)+y(8))-...
y(8)*(dydz(2)+dydz(4)+dydz(6)+dydz(8)))/...
(y(2)+y(4)+y(6)+y(8))^2));
dydz(1) = -d_dens_mix_dz*(y(1)/dens_mix); %Overall mass balance
You might want to check the derivative using symbolic computation:
syms z y10(z) y11(z) y2(z) y4(z) y6(z) y8(z) M1 M2 M3 M4 Rconstant
dens_mix = y10/(Rconstant*y11)*(M1*y2+M2*y4+M3*y6+M4*y8)/(y2+y4+y6+y8);
d_dens_mix_dz = simplify(diff(dens_mix,z))
6 comentarios
I must admit that I don't know why you need a differential equation for u = y(1).
From
d(u*rho) = 0
you get
u*rho = constant
for all z, thus
u(z) = (u*rho)(@z=0) / rho(z) = (m_dot_in/A_in) / rho(z)
Gal Shaked
el 27 de Sept. de 2022
I gave you the derivative of dens_mix with respect to z. I don't understand what you need more to calculate dydz(1).
I also told you that a 13th equation is superfluous because you now know the derivative of dens_mix with respect to z.
The only thing you have to do is copy the code from above and run it in MATLAB.
But as said: According to the continuity equation, rho*u = constant over the length z (assuming that the cross section of the tube remains constant). You know the inflowing mass flow, you know density_mix at position z - thus you know velocity u at position z from the formula I gave you.
Gal Shaked
el 28 de Sept. de 2022
Editada: Gal Shaked
el 28 de Sept. de 2022
how can i use a variable from one MATLAB file in another one? as in the picture, a variable from ODEBVP.m that i want to use in bvp4bc.m
I don't know how the two functions are connected. If nothing helps, use a global variable.
after i solved the velocity u(z), how can i present it in a graph? how do i returning the u(z) as a funciton result? i am presenting the other solved parameters (calculated in ODEBVP.m) in the main file BVP.m just as written below but im not sure how to return the u(z)
You must recalculate density_mix(z) for all z-positions from the other solution variables sol.y and then calculate u from u(z) = (u*density_mix)(@z=0)/density_mix(z)
hi, i know you gave me the 13th equation, the thing is- the code can't run it, it getting stuck by errors.
I didn't give you the 13th equation. I just gave you the formula how to replace -(gradient(dens_mix,z)) in the definition of dydz(1). A 13th equation is not necessary.
Gal Shaked
el 28 de Sept. de 2022
Categorías
Más información sobre Ordinary Differential Equations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


