Matlab - Symbolic computation for a Fourier transform : no result - matlab

In Matlab, I would like to compute the inverse Fourier transform symbolically of the following function :
cf=1/(1 - beta*t*1i)^N
But impossible to get a result with :
>> ifourier(cf,t,x)
ans =
fourier(1/(1 - beta*t*1i)^N, t, -x)/(2*pi)
Isn't there really no explicit formula for the inverse Fourier transform of this function cf
Edit
I try to use sum and product with log to express in other form the cf function :
cf=exp(-A(k)*symsum((1-1i*beta*t),k,1,N)
with N=60 and A=ones(1,N).
But when I apply FT, I get :
cf=exp(-A(k)*symsum((1-1i*beta*t),k,1,N))
Error using message
In 'symbolic:sym:symsum:InvalidSummationIndex', parameter {0} must be a real scalar.
Error in sym/symsum (line 54)
error(message('symbolic:sym:symsum:InvalidSummationIndex', char(x)));
What's wrong here?

Related

Error plotting symbolic variable in MATLAB?

I am trying to learn fourier transform from book"signals and systems laboratory with matlab alex palamides"
On page 312, following code is given which demonstrates that convolution can be implemented by multiplying the fourier transforms of two signals and then taking the inverse fourier of product
syms t w
x1=heaviside(t)-heaviside(t-2);
x2=heaviside(t)-heaviside(t-4);
X1=fourier(x1,w);
X2=fourier(x2,w);
right=ifourier(X1*X2,t)
ezplot(right)
I tried MATLAB 2019 and MATLAB 2020 but i get same problem in both
Actually When i try to run above code in my MATLAB i don't get output like the one in book, instead i get following error
Error using inlineeval (line 14)
Error in inline expression ==> (t.*pi.*sign(t) + fourier(cos(2.*w)./w.^2, w, -t) +
fourier(cos(4.*w)./w.^2, w, -t) - fourier(cos(6.*w)./w.^2, w, -t) - fourier(sin(2.*w)./w.^2, w,
-t).*1i - fourier(sin(4.*w)./w.^2, w, -t).*1i + fourier(sin(6.*w)./w.^2, w, -t).*1i)./(2.*pi)
Undefined function 'fourier' for input arguments of type 'double'.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in ezplotfeval (line 53)
z = feval(f,x(1),y(1));
Error in ezplot>ezimplicit (line 271)
u = ezplotfeval(f, X, Y);
Error in ezplot (line 167)
hp = ezimplicit(cax, f{1}, vars, labels, args{:});
Error in sym/ezplot (line 66)
h = ezplot(fhandle(f)); %#ok<EZPLT>
Error in Untitled (line 7)
ezplot(right)
Snapshot of book page also attached here
I found the same question on MATLAB Answers. The solution as posted by
Walter Roberson is to rewrite X1*X2 in terms of exp before taking the inverse fourier transform. Quoting from MATLAB Answers:
In your release of MATLAB, ezplot() was not compatible with plotting symbolic expressions, and fplot() had to be used instead.
However, the ifourier() is giving unusable results that neither ezplot() nor fplot() can use.
The work-around, valid from R2012a, is:
right = ifourier( rewrite(X1*X2, 'exp'), t);
fplot(right, [0 8])
Result (on R2021b):
It looks like matlab cannot transform the function X1*X2, so it returns the inverse fourier transform as an unevaluated call to fourier.
>> right
right =
(pi*t*sign(t) + fourier(cos(2*w)/w^2, w, -t) + fourier(cos(4*w)/w^2, w, -t) - fourier(cos(6*w)/w^2, w, -t) - fourier(sin(2*w)/w^2, w, -t)*1i - fourier(sin(4*w)/w^2, w, -t)*1i + fourier(sin(6*w)/w^2, w, -t)*1i)/(2*pi)
ezplot, or fplot can not plot a expression like this.

Solving the Linear Regression Model using QR Decomposition (MATLAB)

Background : I want to implement a MATLAB algorithm that takes as input vectors x and y, solves the linear regression problem
associated with the data stored in x and y using a modified QR version and then to plot the graph of the linear function.
So first I wrote the modified QR algorithm :
function x=QRQ(A,b,n)
[Q1,R1]=qr(A);
c1=Q1'*b;
n=length(c1);
x=backward(R1,c1,n);
end
function x=backward(U,y,n)
x=zeros(n,1);
x(n)=y(n)/U(n,n);
for i=n -1 : -1 : 1
x(i)=(y(i)-U(i,i+1 : n)*x(i+1 : n))/U(i,i);
end
end
Then I wrote the algorithm for the linear regression :
function ysol = LinearReg(x,y)
A=[x ones(21,1)];
z=QRQ(A,y,2);
ysol=z(1)*x+z(2);
plot(x,y,'bo',x,ysol,'g-');
end
I tried to run this algorithm on the following data :
x=[0;0.25;0.5;0.75;1;1.25;1.5;1.75;2;2.25;2.5;2.75;3;3.25;3.5;3.75;4;4.25;4.5;4.75;5];
y=[4;3;7;7;1;4;4;6;7;7;2;6;6;1;1;4;9;3;5;2;7];
The full error message that I received is :
Index in position 2 exceeds array bounds (must not exceed 2).
Error in untitled>backward (line 12)
x(n)=y(n)/U(n,n);
Error in untitled>QRQ (line 8)
x=backward(R1,c1,n);
Error in untitled>LinearReg (line 20)
z=QRQ(A,y,2);
The line causing the error is x(n)=y(n)/U(n,n);
the only variable with an index in position 2 is U. Apparently U only has 2 columns, and n is a value >2, hence the error.
Using the debugger, I see that U is a 21x2 array, and n has a value of 21.
How can this MATLAB algorithm be fixed?
The U in your case is R1. Since the matrix A has rank two, R1 will only have two columns since A already only has two columns.
You then try to solve the system R1 * x = y using backward substitution with the index starting at n, but here you clearly have to start at 2.
(Keep in mind that R1 is an upper triangular matrix.)

Compute the differential of function with 2 variables: how to specify the dependance of 2 variables

I try to compute the differential of a function depending of 2 variables : v and t
The symbolic functions are defined as :
d=100;
syms f g h c x v t alpha
alpha=1;
c=4/100
g=c*(v/90)^alpha;
h=diff(g,v);
m=c*(v/90)^alpha*v*t;
%% Derivatives for differential
firstTerm=diff(m,v);
secondTerm=diff(m,t);
firstTermFunction=matlabFunction(firstTerm);
secondTermFunction=matlabFunction(secondTerm);
% Direct computation of integral
f=int(2*d/(2*v^2-h*v^4),v);
vInit=50;
vMax=500;
j=matlabFunction(f);
htMatlab=matlabFunction(f-j(vInit));
v_start=vInit;
step=0.01;
v_final=vMax;
vNew=v_start:step:v_final;
% Function to integrate
htIntegral=#(v)integral(htMatlab,v_start,v);
% Integrate numerically
ht=abs(arrayfun(htIntegral,vNew);
% Take only positive values for ht(vNew) = time
j_sorted=find(ht<10);
t_sorted(1:numel(j_sorted))=ht(j_sorted);
% Set sum of diff to 0
sumDiff=0;
i=0;
while ((sumDistance<d) && (i<(numel(vNew)-1)))
i=i+1;
sumDistance=sumDistance+vLast(i)*(t_sorted(i+1)-t_sorted(i));
sumDiff=sumDiff+firstTerm(vLast(i),t_sorted(i))*(vLast(i+1)-vLast(i))+secondTerm(vLast(i),t_sorted(i))*(t_sorted(i+1)-t_sorted(i));
end
My issue is about the functions firstTermFunction and secondTermFunction :
As you can see, both depend of variables v and t.
I would like to evalute these functions on points t=t_sorted(i) and v_i=vLast(i) but I don't know how to perform this operation, since I don't know how to specify the dependance of v and t for both.
If someone could help me how to evaluate the Matlab function firstTermFunction and secondTermFunction and what syntax to use for that :
I tried to do it by doing directly : firstTermFunction(vLast(i),t_sorted(i)) and secondTermFunction(vLast(i),t_sorted(i)) but I get an error.
Update 1
My issue may come from the conversion of symbolic to numerical function with matlabFunction: indeed, I don't specify the dependence after the conversion.
Update 2
With this syntax:
firstTermFunction=#(v,t) matlabFunction(firstTerm);
I get at the execution :
>> firstTermFunction
firstTermFunction =
function_handle with value:
#(v,t)matlabFunction(firstTerm)
and if I test now the value firstTerm(1,1) (i.e with v=1 and t=1), I get :
>> firstTermFunction(1,1)
ans =
function_handle with value:
#(t,v)t.*sqrt(v.*(1.0./9.0e1)).*(1.0./2.5e1)+t.*v.*1.0./sqrt(v.*(1.0./9.0e1)).*2.222222222222222e-4
This is not what I expect : indeed, variables v and t are not replaced by v=1 and t=1 numerical values.

Error using integral: A and B must be floating-point scalars

I want to evaluate the simple example of integral
a = max(solve(x^3 - 2*x^2 + x ==0 , x));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
and the error is
Error using integral (line 85)
A and B must be floating-point scalars.
Any tips? The lower limit of my integral must be a function, not a number.
The Matlab command solve returns symbolic result. integral accepts only numeric input. Use double to convert symbolic to numeric. As your code is written now, already max should throw an error due to symbolic input. The following works.
syms x;
a = max(double(solve(x^3 - 2*x^2 + x)));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
Output: 1.9331.
the lower limit of my integral must be a function, not a number
integral is a numeric integration routine; the limits of integration must be numeric.
Check values of a by mouse over in breakpoint or removing the ; from the end of the line so it prints a. Based on the error, a is not a scalar float. You might need another max() or double() statement to transform the vector to a single value.
Solve Help : http://www.mathworks.com/help/symbolic/solve.html
Integral Help : http://www.mathworks.com/help/ref/integral.html

Evaluating an integral with implicit function as lower bound

I have this function:
psi = #(x,y)(-(11*x^(6/5)*y^(1/5) - 19*x + 9)/(10*x^(1/10)*y^(1/10)))
Solving for x can only be done implicitly:
R_sp = #(y)solve(psi(x,y), x);
I need to evaluate the following double integral with R_sp as the lower bound:
int(s * int(s*t, t, R_sp(s), 1), s, .3, .4)
When evaluating, matlab says: Error in MuPAD command: A list or a table of entries is expected. [(Dom::Matrix(Dom::ExpressionField()))::create]
The values of R_sp are well-defined in [.3, .4] and can be calculated easily:
R_sp(.3) = 0.845993307852830503940553192774
R_sp(.4) = 0.89641082924669996315283636152907
So I assume that the int function of matlab cannot process continuous values in that implicit function, right? Of course in this case, I can use the trapz function to approximate the integral above by calculating discrete value pairs of the integrand in a for-loop. But is there any other way in matlab to evaluate this integral other than using the trapz approximation (using continuous values)?