Extract the real part of a function as a symbolic expression
12 次查看(过去 30 天)
显示 更早的评论
I want to extract the real part of a known function. Can anyone point out the reason for which Matlab doesn't want to separate the real and imaginary part of the complex function?
clc, clear
syms w e s m R r I b t z b;
k = i^(3/2)*sqrt(i*w*s*m)*sqrt(1+(i*w*e)/s);
J1 = besselj(1,k*R);
J0 = besselj(0,k*r);
funct = real((k*J0*I*exp(i*(w*t+b*z)))/(2*pi*R*J1));
funct
0 个评论
采纳的回答
Walter Roberson
2018-10-19
It cannot figure out what the real and imaginary parts are for that form. For one thing you have not given any constraints on the variables so it must assume they are all complex valued.
But even if you tell it they are all real, you are calling besselj() with parameters that look like they are probably complex (but perhaps might not be, in some circumstances), and besselj() can generally return real or complex values; certainly MATLAB is not going to be able to predict in detail exactly how changes in the various variables are potentially separable into real and complex parts on the output of besselj.
4 个评论
Alex Hanes
2020-4-6
编辑:Alex Hanes
2020-4-6
For specific cases, you can use assume(variable,'real') from the Symbolic Toolbox to define a variable or parameter as real-valued. This might not work for all cases, particularly when you expect solutions can be complex, but if you want to solve something like the full-width at half-maximum of a Gaussian:
where you know
and
are real-valued and
is half of the maximum. Certainly this equation will give 2 real-valued roots, but you get the basic idea.
% FWHM from symbolic
clear; clc;
% Define the Gaussian function centered at t0 symbolically
syms y t t0 tau
y = (tau*sqrt(2*pi))^(-1)*exp(-(t-t0)^2/(2*tau^2));
% Constrain tau = real and tau > 0 (positive)
assume(tau,'real');
assume(tau > 0);
% Find maximum of function from dy/dt = 0
y_max = solve(diff(y,t) == 0);
% Find roots of x for y = y_max/2
x_roots = solve(y == y_max/2,t)
FWHM = x_roots(1,1) - x_roots(2,1)
Walter Roberson
2020-4-6
assume(tau>0) also happens to trigger the assumption of tau being real-valued.
更多回答(1 个)
madhan ravi
2018-10-19
编辑:madhan ravi
2018-10-19
real(funct) %extracts Real part of complex function
imag(funct) %extracts Imaginary part of complex function
7 个评论
Walter Roberson
2018-10-19
syms w e s m R r I b t z b real
does not help in MATLAB for this purpose; the expression is too complicated to find the real and imaginary parts of.
Breaking up into real and imaginary parts would require having a decent closed form formula for besselj, which you are not going to find.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!