How can I simplify a very complicated derivative (when using simplify does not help much)?
7 ビュー (過去 30 日間)
古いコメントを表示
I am working with the following expression:
where
,
,
,
, and
.
My goal is to take the derivative of Π with respect to γ (plugging in and ). Given the complexity of this task, I have written code to do this in MATLAB. Here is my code:
% MATLAB CODE TO CALCULATE THE DERIVATIVE OF PI WITH RESPECT TO GAMMA
% SCRIPT PREPARED ON JANUARY 24, 2024
% Define symbolic variables
syms gamma theta L q alpha s d1 d2 d3 d4 Pi
% Define d1, d2, d3, and d4
d1 = -L + theta^2*(1 - gamma)^2;
d2 = -L + s*gamma^2 - theta^2*(1 - gamma);
d3 = theta^2 + gamma^alpha - theta^2*gamma^2;
d4 = L + theta^2*(1 - gamma);
% Define s
s = ((gamma^2 * d1 * d3 + 2 * gamma^alpha * (1 - L) * d4) + sqrt(gamma^4 * d1^2 * d3^2 - 4 * gamma^(2 * alpha) * (1 - L) * d1 * d4)) / (2 * gamma^(2 + alpha) * (1 - L));
% Define the expression for Pi
Pi = (1 - q*(1 - L)/d1) * (gamma^alpha - gamma^2 * theta^2) + ...
(q*(1 - L)*(s * gamma^2 - theta^2 * (1 - gamma) * (2 - gamma)) / (d1 * d2)) * ((1 - s) * gamma^alpha - gamma^2 * theta^2) + ...
q*(1 - L)/d2 * (gamma^alpha - gamma^2 * theta^2) - ...
q * (theta^2 + gamma^alpha - gamma^2 * theta^2) * log(q*(1 - L)/d2);
% Calculate the derivative of Pi with respect to gamma
dPi_dgamma = diff(Pi, gamma);
% Display the result
disp(dPi_dgamma);
While this works fine, the result is incredibly long and complicated. I wonder if anyone has any advice on how to simplify things further in MATLAB. I find that using the below code does not do anything at all. Any help or advice would be appreciated! Thanks.
% Simplify the derivative
simplified_dPi_dgamma = simplify(dPi_dgamma)
1 件のコメント
Dyuman Joshi
2024 年 1 月 24 日
I doubt there is a short and simple form possible for the obtained expression.
You can try using different name-value arguments of simplify, such as setting the number of steps or the time for simplification, but the results might not be as expceted.
Another thing you can try is changing the output format of the expression by using -
sympref('AbbreviateOutput',false);
This will not abbreviate the output obtained (i.e. there will be no sigmas where terms are grouped).
回答 (2 件)
Torsten
2024 年 1 月 24 日
You can try
nsteps = 100;
simplified_dPi_dgamma = simplify(dPi_dgamma,'Steps',nsteps)
1 件のコメント
Star Strider
2024 年 1 月 24 日
I would use at least:
nsteps = 500;
It will stop when it can either simplify no further or reaches that limit.
Morgan
2024 年 1 月 24 日
I'm assuming alpha, theta, q, and L are constants with respect to gamma. This code provides a bit simplier of a final equation and I used pretty() instead of disp() since it reads a bit better in my experience.
% DEFINE CONSTANTS
syms alpha theta q L
% DEFINE FUNCTIONS OF GAMMA
syms s(gamma) d1(gamma) d2(gamma) d3(gamma) d4(gamma)
% DEFINE PI EXPRESSION
Pi = (1 - q*(1 - L)/d1) * (gamma^alpha - gamma^2 * theta^2) + ...
(q*(1 - L)*(s * gamma^2 - theta^2 * (1 - gamma) * (2 - gamma)) / (d1 * d2)) * ...
((1 - s) * gamma^alpha - gamma^2 * theta^2) + ...
q*(1 - L)/d2 * (gamma^alpha - gamma^2 * theta^2) - ...
q * (theta^2 + gamma^alpha - gamma^2 * theta^2) * log(q*(1 - L)/d2);
% CALCULATE DERIVATIVE
dPi_dgamma = diff(Pi, gamma);
% DISPLAY PRETTY RESULT
pretty(dPi_dgamma);
% CONVERT TO LATEX
chr = latex(dPi_dgamma);
clipboard('copy',chr);
Since d1(gamma), d2(gamma), d3(gamma), and d4(gamma) are relatively easy derivatives to take with respect to gamma it shows the result before factoring those terms out. This script also copies the latex equation to your clipboard and with that I get
Not the prettiest, but the equation isn't pretty to begin with...
8 件のコメント
Morgan
2024 年 1 月 24 日
If you're wanting an analytical expression for to paste into a publication or conference poster/presentation I'd definitely break it up into an expression of the form
then define what you mean by
similar to what I did in previous comments. You might have to do some extra formatting on those equations since latex() and solve() tend to have weird formatting rules. If the intention is to express
you're going to have a very, very, very long expression that will not be very coherent to anyone.
If you just want a code that solves for
given numbers for alpha, theta, q, and L you can use
% DEFINE CONSTANTS
% Replace these with any constant or function
% that is independent of gamma
alpha = 1;
theta = 1;
q = 1;
L = 1;
% DEFINE SYMBOLIC VARIABLE
syms gamma
% DEFINE d1, d3, and d4
d1 = -L + theta^2*(1 - gamma)^2;;
d3 = theta^2 + gamma^alpha - theta^2*gamma^2;
d4 = L + theta^2*(1 - gamma);
% Define s
s = ((gamma^2 * d1 * d3 + 2 * gamma^alpha * (1 - L) * d4) + ...
sqrt(gamma^4 * d1^2 * d3^2 - 4 * gamma^(2 * alpha) * (1 - L) * d1 * d4)) / ...
(2 * gamma^(2 + alpha) * (1 - L));
% DEFINE d2
d2 = -L + s*gamma^2 - theta^2*(1 - gamma);
% DEFINE Pi EXPRESSION
Pi = (1 - q*(1 - L)/d1) * (gamma^alpha - gamma^2 * theta^2) + ...
(q*(1 - L)*(s * gamma^2 - theta^2 * (1 - gamma) * (2 - gamma)) / (d1 * d2)) * ((1 - s) * gamma^alpha - gamma^2 * theta^2) + ...
q*(1 - L)/d2 * (gamma^alpha - gamma^2 * theta^2) - ...
q * (theta^2 + gamma^alpha - gamma^2 * theta^2) * log(q*(1 - L)/d2);
% CALCULATE DERIVATIVE OF Pi
dPi_dgamma = diff(Pi, gamma);
% SOLVE FOR GAMMA*
gamma_star = solve(dPi_dgamma == 0,gamma)
Morgan
2024 年 1 月 24 日
The code I used to get expressions in the first two answers is:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CALCULATE Pi' = f(d1',d2',d3',d4',s')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEFINE CONSTANTS
syms alpha theta q L
% DEFINE FUNCTIONS OF GAMMA
syms s(gamma) d1(gamma) d2(gamma) d3(gamma) d4(gamma)
% DEFINE PI EXPRESSION
Pi = (1 - q*(1 - L)/d1) * (gamma^alpha - gamma^2 * theta^2) + ...
(q*(1 - L)*(s * gamma^2 - theta^2 * (1 - gamma) * (2 - gamma)) / (d1 * d2)) * ...
((1 - s) * gamma^alpha - gamma^2 * theta^2) + ...
q*(1 - L)/d2 * (gamma^alpha - gamma^2 * theta^2) - ...
q * (theta^2 + gamma^alpha - gamma^2 * theta^2) * log(q*(1 - L)/d2);
% CALCULATE DERIVATIVE
dPi_dgamma = diff(Pi, gamma);
% DISPLAY PRETTY RESULT
pretty(dPi_dgamma);
% OPTIONAL CODE TO PRINT LATEX EQUATION
dPi_latex = latex(dPi_dgamma);
fprintf('dPi_dgamma = \n%s\n',dPi_latex)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CALCULATE d1', d2', d3', d4', s'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEFINE d1, d2, d3, d4, AND s
d1 = -L + theta^2*(1 - gamma)^2;
d2 = -L + s*gamma^2 - theta^2*(1 - gamma);
d3 = theta^2 + gamma^alpha - theta^2*gamma^2;
d4 = L + theta^2*(1 - gamma);
s = ((gamma^2 * d1 * d3 + 2 * gamma^alpha * (1 - L) * d4) + ...
sqrt(gamma^4 * d1^2 * d3^2 - 4 * gamma^(2 * alpha) * (1 - L) * d1 * d4)) / ...
(2 * gamma^(2 + alpha) * (1 - L));
% SOLVE AND DISPLAY FOR d1', d2', d3', d4', AND s'
d1p = diff(d1,gamma);
pretty(d1p);
d2p = diff(d2,gamma);
pretty(d2p);
d3p = diff(d3,gamma);
pretty(d3p);
d4p = diff(d4,gamma);
pretty(d4p);
dsp = diff( s,gamma);
pretty(dsp);
% OPTIONAL CODE TO PRINT LATEX EQUATION
d1p_latex = latex(d1p);
fprintf('d1p = \n%s\n',d1p_latex)
d2p_latex = latex(d2p);
fprintf('d2p = \n%s\n',d2p_latex)
d3p_latex = latex(d3p);
fprintf('d3p = \n%s\n',d3p_latex)
d4p_latex = latex(d4p);
fprintf('d4p = \n%s\n',d4p_latex)
dsp_latex = latex(dsp);
fprintf('dsp = \n%s\n',dsp_latex)
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!