How can I differentiate a non linear equation in Matlab

7 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 23 Sep 2015
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 Comments
Walter Roberson
Walter Roberson on 25 Sep 2015
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?
Walter Roberson
Walter Roberson on 25 Sep 2015
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!