Symbolical differentiation of parametric function
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Astrid Barzaghi
el 24 de Jul. de 2023
Comentada: Dyuman Joshi
el 25 de Jul. de 2023
Hello! I am trying to compute the symbolic partial derivative of a parametric function I have defined.
I will report here both main and function
C = [-0.016 1.82e-2 5.5e-3 -2.06e-5 5.91e-7];
G = 6.67259e-20;
R_main = 780e-3;
l = 2;
mass_sys = 5.28e11; %[kg] mass of the system
mass_ratio = 0.0093; % mass ratio
mu = 1 / ((1/mass_ratio) + 1);
m_main = (1 - mu) * mass_sys;
syms x y z
U_main_x = diff(@(x, y, z) U_sph_harm(x, y, z, C, G, m_main, R_main, l), x);
function U = U_sph_harm(x, y, z, C, G, M, R, l)
[lambda, phi, r] = cart2sph(x, y, z);
sum_U = 0;
C_count = 1;
for l_count = 1:l
Plm = legendre(2*l, sin(phi));
for m_count = 0:l_count
sum_U = sum_U + (R/r)^(2*l) * C(C_count) * cos(2 * m * lambda) * Plm(2 * m_count);
C_count = C_count + 1;
end
end
U = G * M / r * (1 + sum_U);
I get the following error
Conversion to logical from sym is not possible.
Error in legendre (line 101)
if ~isreal(x) || ischar(x)|| max(abs(x(:))) > 1
Error in U_sph_harm (line 8)
Plm = legendre(2*l, sin(phi));
Error in main_asteroid (line 92)
U_main_x = diff(@(x, y, z) U_sph_harm(x, y, z, C, G, m_main, R_main, l), x);
Error in sym>funchandle2ref (line 1601)
S = x(S{:});
Error in sym>tomupad (line 1514)
x = funchandle2ref(x);
Error in sym (line 332)
S.s = tomupad(x);
Error in sym/privResolveArgs (line 1102)
argout{k} = sym(arg);
Error in sym/diff (line 29)
args = privResolveArgs(S, invars{:});
Error in main_asteroid (line 92)
U_main_x = diff(@(x, y, z) U_sph_harm(x, y, z, C, G, m_main, R_main, l), x);
Could you please help me sort out why?
Thank you!
0 comentarios
Respuesta aceptada
Dyuman Joshi
el 24 de Jul. de 2023
The function you are using for Legendre polynomials only accepts numerical inputs.
However, there are other errors in your code, particularly in the addition line of double for loop -
Plm(2 * m_count)
It's not clear to me what you want do with this line of code.
As you can see below, that Plm is a function of x, y, and z. So, in case you want to find the value of the function, you will need to provide three inputs to it.
C = [-0.016 1.82e-2 5.5e-3 -2.06e-5 5.91e-7];
G = 6.67259e-20;
R_main = 780e-3;
l = 2;
mass_sys = 5.28e11; %[kg] mass of the system
mass_ratio = 0.0093; % mass ratio
mu = 1 / ((1/mass_ratio) + 1);
m_main = (1 - mu) * mass_sys;
syms x y z
U_main_x = diff(U_sph_harm(x, y, z, C, G, m_main, R_main, l), x)
function U = U_sph_harm(x, y, z, C, G, M, R, l)
[lambda, phi, r] = cart2sph(x, y, z);
sum_U = 0;
C_count = 1;
for l_count = 1:l
%Corrected function
Plm = legendreP(2*l, sin(phi))
for m_count = 0:l_count
% v correction
sum_U = sum_U + (R/r)^(2*l) * C(C_count) * cos(2 * M * lambda) * Plm(2 * m_count);
C_count = C_count + 1
end
end
U = G * M / r * (1 + sum_U);
end
6 comentarios
Dyuman Joshi
el 25 de Jul. de 2023
Ah, I see, it is the Associate Legendre Polynomial. I was not aware of these, that's why I was confused with the notation.
Well you can generate them in this way -
syms x
AP42 = associate(4,2,x)
AP20 = associate(2,0,x)
function AP = associate(l,m,x)
%Definition taken from Wikipedia
AP(x) = (-1)^m*(1-x^2)^(m/2)*diff(legendreP(l,x),x,m);
end
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


