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.
Related
I am trying to optimize the function using fminsearch and function handle
but, I get the error A and B must be floating point scalars. In detalis,
Error using integral (line 86)
A and B must be floating point scalars.
Error in #(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3))
Error in #(x)((x-f2(x,p)).^2)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in #(p)(integral(#(x)((x-f2(x,p)).^2),-3,3))
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
How can I solve this?
i think x-3 become a problem but i cannot deal with it.
x should be variable in f2 in order to do integral with respect to x in q3
Thank you in advance
sigma=0.1;
f2=#(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3));
q3=#(p)(integral(#(x)((x-f2(x,p)).^2),-3,3));
[p, fval] = fminsearch(q3,[0.1 0.4]);
The problem is that when you integrate over x, the function "integrate" gives f2 a vector to evaluate. Which is the same reason you use the dot notation in the function "q3".
The quick fix is to use arrayfun around the "f2" - but you should really consider to adapt to what user20160 suggested in the comments, namely to make full functions, then it will be easier to debug and as it takes in a vector, you can make a for loop, which runs over the inputted endpoints. Standard for-loops are faster than standard arrayfun.
I have code that, when run, should correctly use Lagrange Multipliers to find the maximum/minimum of a function here:
clear all
syms x y L;
f = x^4+2*y^4;
g = x^2+5*y^2+2*y^2-10;
firstpart=jacobian(f,[x y])-L*jacobian(g,[x y]);
[Lsoln,xsoln,ysoln]=solve(firstpart,x^2+5*y^2+2*y^2-10);
subs(f,{x,y},{xsoln,ysoln})
% The coordinates that correspond with the greatest and smallest values
% above are the maximum and minimum, respectively.
However, when I run it, I get four errors:
Error using sym.getEqnsVars>checkVariables (line 92) The second
argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 62)
checkVariables(vars);
Error in solve>getEqns (line 450) [eqns, vars] =
sym.getEqnsVars(argv{:});
Error in solve (line 225) [eqns,vars,options] = getEqns(varargin{:});
Could anyone help?
You are passing two equations as individual arguments zu solve, that is not possible. You have to put both into an array
[Lsoln,xsoln,ysoln]=solve([firstpart,x^2+5*y^2+2*y^2-10] );
I'm trying to write some matlab code. Why won't my new variable w get accepted so that my function can take another parameter? I want to iterate over the variable that is now 7.8 and use iteration instead.
function dZ=sys(x,Z,w)
c=#(z)4800 - 20.2090 + (17.3368)*z/1000+ (272.9057)*exp(-z*0.7528/1000); % c(z)
c=c(2000);
deg=w;
% Z(1):=z
% Z(2):=u
dZ=zeros(2,1); % a column vector
dZ(1)=Z(2);
dZ(2)=-(c/cosd(7.8))^2*(((-272.9057*0.7528/1000)*exp(-Z(1)*0.7528/1000)) + 17.3368/1000)/...
(4800 - 20.2090 + (17.3368)*Z(1)/1000+ (272.9057)*exp(-Z(1)*0.7528/1000))^3;
end
I get an error message when trying to use the new variable in my function that worked before.
Error using sys (line 4)
Not enough input arguments.
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in underwater (line 2)
[X,Z]=ode45(#sys,x,[2000 tand(7.8)], 7.8);
I would write the function as follows (although w doesn't appear to be used):
function dZ=sys(t,Z,w)
z = 2000;
c = 4800 - 20.2090 + (17.3368)*z/1000+ (272.9057)*exp(-z*0.7528/1000);
deg=w; % not used?
dZ=zeros(2,1); % a column vector
dZ(1)=Z(2);
dZ(2)=-(c/cosd(7.8))^2*(((-272.9057*0.7528/1000)*exp(-Z(1)*0.7528/1000)) + 17.3368/1000)/...
(4800 - 20.2090 + (17.3368)*Z(1)/1000+ (272.9057)*exp(-Z(1)*0.7528/1000))^3;
end
and then call the ode solver as follows:
[T,Z_sol] = ode45(#(t,Z) sys(t,Z,w),x,[2000 tand(7.8)]);
where w and x are defined in your base or caller workspace.
The ODE solver is expecting a function of two variables. It is looking for a function in the form of:
y' = f(t, y)
So, if you are passing #sys to ode45, it must be a function of only two variables.
In the comments below you reference another question. They use the following call:
[t,N] = ode45(#(t,y) rateEquations(t,y,F), timeSpan, initialConditions)
Using #(t,y) makes a generic function which is a function of two variables. In this example the function rateEquations is called with a fixed F value. So ODE is in fact expecting a function of two variables. If you have a function of more than two variables you can recast it as a function of two variables by copying the technique used in the example you reference. Hope this helps!
I try to plot these 2 graphs on the same plot but matlab return error 'can't divide by zero' and refer me to sinc of 0.
I don't know what to do bc sinc(0)=1, I don't understand the problem.
my code:
syms x
ezplot(heaviside(x+1) - heaviside(x-1), [-2, 2])
hold
t=-2:0.1:2;
syms k
r=symsum( ((sinc(k/2)/2)*exp((1i)*k*pi*(t/2))), -1,1);
plot(t,r)
problem:
??? Error: File: aa.m Line: 6 Column: 18
Unexpected MATLAB expression.
Current plot held
??? Error using ==> mupadmex
Error in MuPAD command: Division by zero [_power];
during evaluation of 'sum::sum'
Error in ==> sym.symsum at 74
r = mupadmex('symobj::map',f.s,'symobj::symsum',x.s,a.s,b.s);
Error in ==> aa at 6
r=symsum( ((sinc(k/2)/2)*exp((1i)*k*pi*(t/2))), -1,1);
Use this alternative definition for sinc:
ssinc=#(X)(1./(gamma(1+X).*gamma(1-X)))
syms x
ezplot(heaviside(x+1) - heaviside(x-1), [-2, 2])
hold
t=-2:0.1:2;
syms k
r=symsum( ((ssinc(k/2)/2)*exp((1i)*k*pi*(t/2))), -1,1);
plot(t,r)
This code uses an alternative definition of the sinc function:
(Source: Wikipedia)
Another solution, instead of using an alternative definition with the gamma function, I added a correction to redefine the x=0 point.
The original function has a 0/0 situation, I redefined it using a correction function with correction(0)=1 and correction(1)=0 otherwise. This changes to function to 1/1 at sinc(0).
%correction(0)=1, correction(x)=0 otherwise. A little bit idiotic, but I'm unable to define this in a simpler way which is compartible to the symbolic toolbox:
correction=#(x)(((heaviside(x)-.5).^2)-.25)*-4
%redefine sinc using the original function, but use correction(x) to fix sinc(0)
ssinc=#(x)((sin(pi*x)+correction(x))./((pi*x)+correction(x)))
syms x
ezplot(heaviside(x+1) - heaviside(x-1), [-2, 2])
hold
t=-2:0.1:2;
syms k
r=symsum( ((ssinc(k/2)/2)*exp((1i)*k*pi*(t/2))), -1,1);
plot(t,r)
I am trying to solve a problem (think excel "goal seek") where given a specific rate of return, matlab can solve for the variable "y" (which is used to multiply a matrix).
I've tried using fgoalattain (i don't understand it) and solve
here is the function which basically adds some arrays together to make a cashflow with y being a double. ppam_gen is a n*1 matrix. other_tot is a n*1 matrix.
down_pmt is a double.
function ansirr = cirr(y)
cf_gen_oth = y*ppam_gen + oth_tot
dp = [-down_pmt]
cashflow = [dp;cf_gen_oth]
ansirr = irr(cashflow)
%ansirr = irr([dp; y*ppam_gen + oth_tot])
end
x = fgoalattain(#cirr,0.001,.15,abs(.15))
does not work, the following error is given:
Error using roots (line 28)
Input to ROOTS must not contain NaN or Inf.
Error in irr (line 134)
coeff = roots(fliplr(cf(:,loop)')); % Find roots of polynomial
Error in ppa_model/cirr (line 139)
ansirr = irr(cashflow)
Error in goalcon (line 26)
f = feval(funfcn{3},x,varargin{:});
Error in
fgoalattain>#(y,varargin)feval(cfun{3},y,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,errCheck,varargin{:})
(line 473)
cfun{3} = #(y,varargin)
feval(cfun{3},y,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,errCheck,varargin{:});
Error in nlconst (line 746)
[nctmp,nceqtmp] = feval(confcn{3},x,varargin{:});
Error in fgoalattain (line 519)
[xnew,ATTAINFACTOR,LAMBDA,EXITFLAG,OUTPUT]=...
Error in ppa_model (line 152)
r = fgoalattain(#cirr,0.001,.15,abs(.15))
and using symbolic toolbox doesn't work either.
syms y
solve(irr([-down_pmt ; y*ppam_gen + oth_tot]) == 0.15)
throws the error:
Error using assignin
Attempt to add "y" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in syms (line 66)
assignin('caller',x,sym(x));
Error in ppa_model (line 152)
syms y