Simplifying with respect to trig identities to avoid dividing by zero
2 次查看(过去 30 天)
显示 更早的评论
I need to calculate the jacobian for a set of differential equations and the calculation with respect to one variable is particularly complicated.
A working example
syms x y v a psi omega dt
state = [x; y; v; a; psi; omega];
wt = omega * dt / 2;
hwt = psi + wt;
fx = state + [
(v*dt + 0.5*a*dt^2)*cos(hwt)*usinc(wt) + a*dt/omega*sin(hwt)*(cos(wt) - usinc(wt));
(v*dt + 0.5*a*dt^2)*sin(hwt)*usinc(wt) + a*dt/omega*cos(hwt)*(usinc(wt) - cos(wt));
a * dt;
0;
omega * dt;
0
];
dfx_domega = diff(fx(1), omega); % this is the derivative to be simplified
function [ y ] = usinc( x )
%un-normalized sinc function
i=find(x==0);
x(i)= 1; % From LS: don't need this is /0 warning is off
y = sin(x)./(x);
y(i) = 1;
end
The problem is that the derivative results in fractions with ω in the denominator. For this case this is the turn rate which can very possibly be zero, as such I would like to manipulate the equations such that it is not in the denominator as far as possible, this is possible in large by applying .
Is there any way I can apply simplify or rewrite such that the fractions are simplified as such?
0 个评论
回答(1 个)
Hernia Baby
2021-2-27
How about using logical indexing?
function [ y ] = usinc( x )
y = sin(x)./(x); % y(x==0) => NaN
y(x==0) = 1; % y(x==0) => 1
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!