MATLAB won't invert a function in the Laplace domain...

47 次查看(过去 30 天)
We are using a fairly simple function of the gamma distribution. When I use whole numbers for the "n" parameter, MATLAB inverts the function well. However, when I use non-integer values for n (e.g., n=11/10 or 1.1), MATLAB is unable to invert the function.
Here is the error message:
Warning: Error in state of SceneNode. The following error was reported evaluating the function in FunctionLine update: Unable to convert symbolic expression to double array because it contains symbolic function that does not evaluate to number. Input expression must evaluate to number.
I want to be able to use various values of n that are not integers.
Here is my code.
Do I need to use a numerical inversion method? or is there a simple means of fixing this withing MATLAB?
% Clear all variables and close all plots.
clc
close all;
clear all;
% Declare the variables that are symbolic.
syms t s N n m r t_close tbar
syms E_t E_s Day_hours
syms C_a_s C_a_t Fun_s
% Define the values of the model parameters
tbar=8; % tbar is one of the gamma distribution parameters.
n=1; % n is the second of two parameters needed to
% define the gamma distribution shape. set n=1.1, and it
% does not work.
Day_length=24;
r=0.5;
t_close=8;
m=sym(tbar/t_close);
% N is the number of cycles for which we
% want this to run. For now, one cycle is
% enough to determine whether this will work.
for N=1
% Define the gamma distribution
E_t(N)=((1/tbar)*(((t)/tbar)^(n-1))*((n^n)/gamma(n))*exp(-n*(t)/tbar));
% Transform the gamma distribution into the Laplace domain
E_s(N)=laplace(E_t(N));
% Define a function that is a result of a derivation
Fun_s(N)=(exp(-Day_length*(N-1)*s))/s-exp((-Day_length*(N-1)-t_close)*s)/s;
% Define the concentration function in the Laplace domain
C_a_s(N)=(m/(tbar*s))*((1-E_s(N))/(1-r*E_s(N)))*Fun_s(N);
% Now, take the inverse laplace of the concentration function to
% put it in the time domain.
% if n is not an integer, it won't invert the function.
% any ideas???
C_a_t(N) = ilaplace(C_a_s(N),s,t)
fplot(C_a_t(N),[0 60])
end
C_a_t = 
  2 个评论
John
John 2025-9-17,15:49
How about for values where n is not an integer?
John
John 2025-9-17,15:53
I failed to copy the "end" statement to the code. I have that on my original code. But when I use a value of n other than an integer, it fails to invert into the time domain. Any ideas?

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2025-9-17,15:28
编辑:Star Strider 2025-9-17,20:20
After running your code (with n=1.1), it seems that the problem is that using a non-integer value for 'n' produces non-integer exponents. While is it is possible to transform time expressions with non-integer exponents into Laplace space, conbing those with other expressions in Laplace space produces a result that cannot be inverted, even using partfrac and simplify (with a 500-iteration limit).
To evaluate 'C_a_t', I created a short 't' vector with linspace and then used the subs function. That never produced a numerical result.

Umar
Umar 2025-9-18,0:50

Hi @John,

I took a look at your gamma distribution Laplace inversion problem, and you've hit a classic limitation of MATLAB's symbolic inverse Laplace transform capabilities. The issue arises because non-integer values for the 'n' parameter produce non-integer exponents, and while MATLAB can transform time expressions with non-integer exponents into Laplace space, combining those with other expressions creates results that cannot be analytically inverted, even with partfracand simplify functions.

So, the root problem that we are dealing here with, when n is non-integer (like 1.1), your gamma distribution expression:

E_t(N)=((1/tbar)*(((t)/tbar)^(n-1))*((n^n)/gamma(n))*exp(-n*(t)/tbar));

produces fractional powers that create complex symbolic expressions in the Laplace domain. The symbolic ilaplacefunction simply can't handle the analytical inversion of such expressions when they're combined with your other terms in C_a_s(N).

My suggestion would be at this point for you to try switching to numerical inverse Laplace methods. Here are your best options:

1. Numerical Inverse Laplace Transform Toolbox MATLAB File Exchange offers numerical inverse Laplace transform methods including Euler and Talbot algorithms, with both regular and symbolic versions for higher precision.

Download from: https://www.mathworks.com/matlabcentral/fileexchange/39035-numerical-inverse-laplace-transform

2. Gaver-Stehfest Algorithm The Gaver-Stehfest algorithm is specifically designed for inverse Laplace transforms of arbitrary functions and works well with gamma distributions.

Available at:

3. Modified Code Approach Here's how you could modify your approach: % After computing C_a_s(N), use numerical inversion instead of ilaplace % Download one of the numerical inverse Laplace functions first

% For example, using Talbot method:
% Define time vector
 t_vec = linspace(0, 60, 1000);
% Convert symbolic expression to function handle
C_a_s_func = matlabFunction(C_a_s(N), 'Vars', s);
% Apply numerical inverse Laplace
for i = 1:length(t_vec)
  C_a_t_numerical(i) = talbot_inversion(C_a_s_func, t_vec(i));
end
% Plot results
plot(t_vec, C_a_t_numerical);

The alternative semi-analytical approach for you, if you want to stay closer to analytical methods, consider:

1. Using rational approximations for the gamma function with non-integer parameters 2. Breaking your problem into simpler components that can be inverted separately 3. Using series expansions around integer values of n

As far as performance considerations goes, the Talbot method is generally high accuracy and fast, but can fail catastrophically for certain time-domain behaviors like Heaviside step functions or some oscillatory behaviors. The Gaver-Stehfest method tends to be more robust for a wider range of functions. I'd recommend starting with the Gaver-Stehfest algorithm since it's specifically designed for cases like yours where analytical inversion fails.

References:

Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by