Convolution in matlab - matlab

I'm trying to calculate this convolution:
x[n] = δ[n+1] + δ[n] - δ[n-1]
h[n] = (1/2)^n * u[n] . u[n] is the step function.
Here's my code:
>> n=[-10:10];
>> x=zeros(1,length(n));
>> x(n==-1)=1;
>> x(n==0)=1;
>> x(n==1)=-1;
>> u=heaviside(n);
>> h=(1/2).^n * u;
??? Error using ==> mtimes
Inner matrix dimensions must agree.
How do you exactly type my h[n]? What if it's u[n-1] instead?

>> n=[-10:10];
>> x=zeros(1,length(n));
>> x(n==-1)=1;
>> x(n==0)=1;
>> x(n==1)=-1;
>> u=heaviside(n);
>> h=(1/2).^n .* u; %Note the element wise operation .*
>> conv(x,h)

Related

Multiple anonymous function MATLAB

My goal is to compute the derivative of a function and use this result as another function. Clearly, it means :
f = #(x) (x-1)*(x-2); %A simple function
derivative = jacobian(f,x) %MATLAB output : "2*x - 3"
df = #(x) derivative %= #(x) 2*x - 3
df(2) %= "2*x -3" instead of 2*2 - 3
How can I do such a thing ? I tried syms x but it doesn't help.
You want matlabFunction:
g = matlabFunction(f) converts the symbolic expression or function f to a MATLAB function with handle g.
In your example:
>> syms x
>> f = #(x) (x-1)*(x-2);
>> derivative = jacobian(f,x);
>> df = matlabFunction(derivative)
df =
function_handle with value:
#(x)x.*2.0-3.0
>> df(2)
ans =
1

Substitute symbolic variables with vectors

Consider the script
syms a b;
f = a.*b;
a = [1 2];
b = [0 2];
subs(f)
This yields a vector as output ([0, 4]), but the intended output is a scalar (4), given the element-wise multiplication that should be performed.
How can I properly utilize vectors when substituting symbolic functions?
I believe you're making two mistakes. The first is that you're overwriting your symbolic variables with double arrays, so you're not actually calling subs for a symbolic object:
>> syms a;
>> class(a)
ans =
sym
>> a = [1 2];
>> class(a)
ans =
double
The second is that preventing this will still give a wrong answer:
>> syms a b;
>> f = a.*b
f =
a*b
>> subs(f,{a,b},{[1, 2], [0,2]})
ans =
0 4
That's because, as you see in the printed version of f, the symbolic engine treats a and b as scalars.
So to do what you probably want to do you need to define your syms to be 2-element arrays:
> a = sym('a',[1 2])
a =
[ a1, a2]
>> b = sym('b',[1 2])
b =
[ b1, b2]
>> f = a.*b
f =
[ a1*b1, a2*b2]
>> subs(f,[a,b],[[1,2],[0,2]])
ans =
0 4
But anyway, as you can see, the result is still an array since .* (or symbolic *) is elementwise multiplication. In order to get a scalar product, use sym/dot:
>> dot(a,b)
ans =
b1*conj(a1) + b2*conj(a2)
>> subs(dot(a,b),[a,b],[[1,2],[0,2]])
ans =
4

How to assemble a new function out of existing functions?

I have a function kappa that contains two other functions sigma_ and sigma__.
The error I get is
error: binary operator '.^' not implemented for 'function handle' by 'scalar' operations
error: called from
at line -1 column -1
My code is
>> syms epsilon
>> a = 0.36990
>> b = 2.6474
>> sigma = #(epsilon) 10 .^ (a * log (epsilon) + b)
>> sigma_=#(epsilon) diff(sigma)
>> sigma__=#(epsilon) diff(sigma_)
>> kappa=#(epsilon) (sigma__)/(1+sigma_.^2).^(3/2)
>> kappa(1)
error: binary operator '.^' not implemented for 'function handle' by'scalar' operations
error: called from
at line -1 column -1
I edited my code:
>> sigma_ = #(epsilon) diff (sigma (epsilon))
>> sigma__=#(epsilon) diff(sigma_(epsilon))
>> sigma_(1)
ans = [](0x0)
>> kappa=#(epsilon) (sigma__(epsilon))/(1+sigma_(epsilon).^2).^(3/2)
>> kappa(1)
ans = [](0x0)

Strange error invoking ilaplace function in matlab

I am using ilaplace transform in matlab to compute the inverse Laplace transform of a function, however, I meet a strange error I cannot handle. Here is the output from matlab command line, where >> is the prompt. (s and t are syms symbolic variables)
>> N
N =
-(2.7071747341794232778783099808678e-34*(3.5938879023008902403763863659998e33*s - 68267793397927900578281423804583.0))/s^2
>> ilaplace(N)
Error using mupadmex
Error in MuPAD command: The argument is invalid. [_concat]
Evaluating: partfrac
Error in transform (line 74)
F = mupadmex('symobj::vectorizeSpecfunc', f.s, x.s, w.s, trans_func,
'infinity');
Error in sym/ilaplace (line 31)
F = transform('ilaplace', 's', 't', 'x', L, varargin{:});
>> ilaplace(-(2.7071747341794232778783099808678e-34*(3.5938879023008902403763863659998e33*s - 68267793397927900578281423804583.0))/s^2)
ans =
(23990078920530555628928726307903*t)/1298074214633706907132624082305024 - 4933332333844707562298796153537/5070602400912917605986812821504
The prblem is, if I directly invoke ilaplace(N), I have an error. However, if I pass the expression to ilaplace, I have the right answer. I don't know why.
EDIT: The complete code file is as follows.
syms s t;
syms s positive;
syms t positive;
tau = 4.2562313045743237429846099474892;
V_fitted = 1 - 1.015*exp(-0.0825*t); % CDF function
V_fitted_right_shift = subs(V_fitted, t, t - tau); % right shift CDF by tau
lambda_out = 1 / ( tau + int((1 - V_fitted_right_shift), [tau, Inf]) ); %arrival rate of the process
K = 4; %number of processes
V_CDF_superposed = 1 - (1 - V_fitted_right_shift)*(lambda_out * int((1-V_fitted_right_shift), [t-tau, Inf]))^(K - 1); %new CDF
FV_CDF_superposed = vpa(V_CDF_superposed);
Laplace_V_CDF_superposed = laplace(FV_CDF_superposed); %laplace transform
N = Laplace_V_CDF_superposed/(1 - s * Laplace_V_CDF_superposed);
N = vpa(N);
N = simplify(N);
N_t = ilaplace(N);

How do I differentiate and evaluate in Matlab?

I need to differentiate exp((s^2*sigma^2)/2 + mu*s) and evaluate it to s=0.
Anyone can provide advice on the syntax or how I should go about performing this differentiation?
If it helps, the above function is the mgf of a standard normal.
I want to differentiate and evaluate s=0 so I can get the mean,variance,skew and kurtosis.
Thanks!
Try this :
>> syms s sigma mu
>> f=diff(exp((s^2*sigma^2)/2 + mu*s),s)
f =
exp((s^2*sigma^2)/2 + mu*s)*(s*sigma^2 + mu)
>> subs(f, 's', 0)
ans =
mu
>> subs(f, 's', 1)
ans =
exp(sigma^2/2 + mu)*(sigma^2 + mu)
Ref - diff, subs