How can I differentiate a non linear equation in Matlab

y = 5.0*10^-10*x +7.0*10^-25*x^3
I need to solve equation above for (x) , then differentiate (getting jacobian) for (y)
like:
sol ={solve(y,x)}
diff1= diff(sol,y)
Is that possible?

 Risposta accettata

Yes, just use
syms x
y = 5.0*10^-10*x +7.0*10^-25*x^3
sol = solve(y, x)
diff(sol, y)
But remember that there are multiple solutions for x because there are three roots to the cubic.

5 Commenti

Thanks Walter,
it returns this error
Error using sym/diff (line 67) The second argument must be a variable or a nonnegative integer specifying the number of differentiations.
syms x y
z = y == 5.0*10^-10*x +7.0*10^-25*x^3
sol = solve(z, x)
diff(sol, y)
ND
ND il 24 Set 2015
Modificato: ND il 24 Set 2015
Yes that's right but please what about forth order if the power likes x^4 and I would put that diff in a FORTRAN file. It would give me an error!
Error using mupadmex Error in MuPAD command: Code generation failed due to an unexpected object of type 'RootOf'. [generate::CFany]
Also, if the equation is generated in Matlab code as generated formula and I can not imported it as above ?
syms x y
z = y == ( equation is in m.file) as generated formula
sol = solve(z, x)
diff(sol, y)
Many thanks for your help
For those particular parameters, the Fortran code for the real root
Root1 = 0.52920000000000D14 * ((dble(52920 * y) + 0.42D2 * sqrt(dble
#(1587600 * y ** 2 + 42))) ** (0.2D1 / 0.3D1) + 0.42D2) * (dble(y)
#+ sqrt(dble(1587600 * y ** 2 + 42)) / 0.1260D4) * (dble(52920 * y)
# + 0.42D2 * sqrt(dble(1587600 * y ** 2 + 42))) ** (-0.4D1 / 0.3D1)
# * dble((1587600 * y ** 2 + 42) ** (-0.1D1 / 0.2D1))
Part of the difficulty is that you have not restricted yourself to real roots. The code generation tools I use for Fortran have trouble with long expressions that involve complex numbers :(
I did some tests with
y == 5*10^(-10)*x +7*10^(-25)*x^3 + A * x^4
and it appears to me that Fortran does not have the precision needed to evaluate the roots.
Is it a particular 4-degree polynomial that you will be working with, or do you need to run through a bunch of them?
You mean like
fortran(S,'file',fileName)
You can construct the general pattern for order 3 equations
syms x y A B C D
z = y == A * x.^3 + B * x.^2 + C * x + D;
sol = solve(z,y);
dsol = simplify(diff(sol(1),y));
fortran(dsol, 'filename', 'dinvcubic.f')
Once generated you would only need to invoke it after assigning appropriate values to A, B, C, D
The same technique can be used for order 4, but the expressions get very long, and the code might not have enough precision for accurate answers for some ranges.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by