Using matlab to find Laplace transform - matlab

f(t) = t*e^t when 0 <= t < 3
f(t) = 0 when 3 <= t
how to use Matlab to find Laplace transforms of functions which change according to different values of t

MATLAB has a function called laplace, and we can calculate it like:
syms x y
f = 1/sqrt(x);
laplace(f)
But it will be a very long code when we turn f(x) like this problem into syms.
Indeed, we can do this by using dirac and heaviside if we have to. Nevertheless, we could use this instead:
syms t s
f=t*exp((1-s)*t);
F=int(f,t,0,3)
It is because:

If you are interested in a numerical implementation of the Laplace transform, you can download from Matlab's file exchange the following Numerical Transform and the inverse transform ...

Related

Matlab cart2pol function

Does the Matlab's cart2pol() function calculate the value of theta differently? Here's an example i worked out when converting to polar coordinates using cart2pol.
First, i implement it with cart2pol.
N = 8;
x = 1:N; y = x;
[X,Y] = meshgrid(x,y);
[theta,rho] = cart2pol(X-floor(N/2),-(Y-floor(N/2)))
which gives
Now, if i use the equation which is:
Note in Matlab, -1 and +1 is not required
theta = atan2((N-2.*Y),(2.*X-N))
i get:
Why is it that the value 3.1416 is negative for the cart2pol function and positive based on the equation?
As beaker said, the angle is the same. But regarding your comment: You can always use edit <functionname> to see how the Matlab function is implemented. This usually works for .m files, but not for P and MEX files, since these are binary and therefore can't be edited directly. So
edit cart2pol
in this case and you'll see that all cart2poldoes, is just atan2(y,x) to get theta. So this means that the different results are just due to the fact that you call the function using different inputs than the inputs you use in your "formula".
atan2(-(Y-floor(N/2)),X-floor(N/2))
gives exactly the same result as your call of cart2pol.

laplace transform of dirac delta function in matlab

My question is about the difference between these two definitions of dirac delta function in Matlab: dirac(t,1) and dirac(t-1) I tried to apply the laplace transform and inverse laplace transform to these 2 functions and they give me very different results:
syms s t
F_s=s+s^2; % definition of the function in s domain
f_t=ilaplace(F_s)
f_t =
dirac(t, 1) + dirac(t, 2)
F_s=laplace(f_t)
F_s =
s^2 + s
F_s=laplace(dirac(t-1)+dirac(t-2)) % but if I use this definition for dirac delta function it gives a very diffrent answer...
F_s =
exp(-s) + exp(-2*s)
and my problem is that when i want to plot the function as:
t=1:0.1:4;
f_t1=dirac(t, 1) + dirac(t, 2);
Error using double.dirac Too many input arguments.
It can't plot the function so I used the definition as :
dirac(t-1)+dirac(t-2) but it gives a very different answer in Laplace transform... could you please explain the reason to me?
Thank you all
sahar

How to perform indefinite integration of this function in MATLAB?

I need to perform the following operations as shown in the image. I need to calculate the value of function H for different inputs(x) using MATLAB.
I am giving the following command from Symbolic Math Toolbox
syms y t x;
f1=(1-exp(-y))/y;
f2=-t+3*int(f1,[0,t]);
f3=exp(f2);
H=int(f3,[0,x]);
but the value of 2nd integral i.e. integral in the function H can't be calculated and my output is of the form of
H =
int(exp(3*eulergamma - t - 3*ei(-t) + 3*log(t)), t, 0, x)
If any of you guys know how to evaluate this or have a different idea about this, please share it with me.
Directly Finding the Numerical Solution using integral:
Since you want to calculate H for different values of x, so instead of analytical solution, you can go for numerical solution.
Code:
syms y t;
f1=(1-exp(-y))/y; f2=-t+3*int(f1,[0,t]); f3=exp(f2);
H=integral(matlabFunction(f3),0,100) % Result of integration when x=100
Output:
H =
37.9044
Finding the Approximate Analytical Solution using Monte-Carlo Integration:
It probably is an "Elliptic Integral" and cannot be expressed in terms of elementary functions. However, you can find an approximate analytical solution using "Monte-Carlo Integration" according to which:
where f(c) = 1/n Σ f(xᵢ)
Code:
syms x y t;
f1=(1-exp(-y))/y; f2=-t+3*int(f1,[0,t]); f3=exp(f2);
f3a= matlabFunction(f3); % Converting to function handle
n = 1000;
t = x*rand(n,1); % Generating random numbers within the limits (0,x)
MCint(x) = x * mean(f3a(t)); % Integration
H= double(MCint(100)) % Result of integration when x=100
Output:
H =
35.2900
% Output will be different each time you execute it since it is based
% on generation of random numbers
Drawbacks of this approach:
Solution is not exact but approximated.
Greater the value of n, better the result and slower the code execution speed.
Read the documentation of matlabFunction, integral, Random Numbers Within a Specific Range, mean and double for further understanding of the code(s).

How to perform convolution using Fourier Series

OK let me cut to the chase.
I am trying to use MATLAB to
(i)generate the fourier series based on known coefficients and thereafter
(ii) determine the output function when the impulse is known.
So far I used this code to obtain the fourier series:
clear all
syms x k L n
evalin(symengine,'assume(k,Type::Integer)');
a = #(f,x,k,L) (2/(pi*k))* sin((pi*k)/(2 * L));
fs = #(f,x,n,L) (1/2*L) + symsum(a(f,x,k,L)*cos(k*2*pi*x/L),k,1,n);
f = x;
pretty(fs(f,x,11,1))
This works as desired. Now the impulse response is as follows:
h = heaviside(x) * exp(-5*x);
Now, in order to obtain the function, we need to perform the convolution with the respective functions.But when I input the following, I get the error:
x1 = fs(f,x,1,1);
conv(h,x1)
Undefined function 'conv2' for input arguments of type 'sym'.
Error in conv (line 38)
c = conv2(a(:),b(:),shape);
Any help would be appreciated
That is because conv is only defined for numeric inputs. If you want to find the convolution symbolically, you'll have to input the equation yourself symbolically using integration.
If you recall, the convolution integral is defined as:
Source: Wikipedia
Therefore, you would do this:
syms x tau;
F = int(h(tau)*x1(x-tau),'tau',-inf,+inf);
int is a function in MATLAB that does symbolic integration for you. Also note that the convolution integral is commutative, and so this also works:
Source: Wikipedia
Therefore, you should also get the same answer if you did:
syms x tau;
F = int(h(x-tau)*x1(tau),'tau',-inf,+inf);
Hope this helps!

How to evaluate emprical cdf at given points in Matlab?

Suppose I have a sequence of scalar points subject to a unknown distribution.
From the sequence of points, we can get the empirical cdf.
I was wondering if there is some way in Matlab to evaluate this empirical cdf at any point? For example, evaluate it at the same sequence of points that are used to build the empirical cdf?
I have looked up the function ecdf at http://www.mathworks.com/help/stats/ecdf.html. Its usage is [f,x] = ecdf(y), where the empirical cdf from data yis evaluated atx, butx` doesn't seem to be specifiable.
Thanks and regards!
Assuming you have the output of the function, two vectors f and x and you want to find the emperical cdf at point x_of_interest, this is what you can do:
max(f(x<=x_of_interest))
Or maybe you want to use minand >=, but I think the above formula is correct.
It seems like x are unique points in y with their CDF.
I'm not sure that's what you meant, but I also needed to convert a vector of data values to the corresponding vector of empirical CDFs, with the same ordering.
Actually, instead of the usual definition cdf(x) = Prob(X <= x) I prefer the more symmetric definition cdf(x) = Prob(X < x) + 1/2 * Prob(X == x) , which is more suitable to cases with ties. Now the computation is a one-liner, but with the help of the tiedrank() function from the statistical toolbox:
cdf = (tiedrank(data) - 1/2) / length(data) ;
For example,
data = [3 2 4 2 1] ;
yields
cdf = [0.7 0.4 0.9 0.4 0.1] ;
A good approach might be to use interpolation to find the closest "x" for each point you want to evaluate, and the relative "f" value. You can find how to use interp1 for this purpose here:
determining-the-value-of-ecdf-at-a-point-using-matlab