The following error was reported evaluating the function in FunctionLine update: Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

Hi! I am trying to plot a fourier function by using fplot but I am getting the following error.
The following error was reported evaluating the function in FunctionLine update: Unable to convert expression containing symbolic variables into double array.
Apply 'subs' function first to substitute values for variables.
syms f(t) g(t) w;
x=@(t)heaviside(exp(-20*t));
h=@(t) heaviside(12.*t.*exp(55*t));
xf=vpa(fourier(f,t,w));
hf=vpa(fourier(g,t,w));
fplot(@(w) abs(hf), [-20*pi 20*pi])
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
Warning: Error updating FunctionLine.

The following error was reported evaluating the function in FunctionLine update: Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

11 Kommentare

Why did you edit away your question and delete your comment? That is very rude. Luckily Google cache still contains the original question and the comment.
If you keep removing the code from your question I will keep putting it back (until a Mathworks employee tells me otherwise). I bet I'm more stubborn than you are.
Why should I keep this when i did not get a proper ansver?
Out of respect for the response you did get. Walter spent time reading your question, understanding your problem and writing a suggested solution. If that is not the complete solution you can follow up with comments.
You should also note that some questions don't have proper answers. That is also valuable knowledge.
I showed my respect by saying thank you and I could not solve my problem so there is no need to keep this in here. Moreover I dont want people to get confused with that unsolved problem. Is that enough for removing that. By the way I also did not want to be disrescpect to you that issue more about my bad languange.
If you can solve this, please let me know because i immediately need this
So you still want an answer, even when you delte the question? Sorry, but this is just rude on your part.
The title for this question was:
The following error was reported evaluating the function in FunctionLine update: Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
The question itself was:
Hi! I am trying to plot a fourier function by using fplot but I am getting the following error.
The following error was reported evaluating the function in FunctionLine update: Unable to convert expression containing symbolic variables into double array.
Apply 'subs' function first to substitute values for variables.
syms f(t) g(t) w;
x=@(t)heaviside(exp(-20*t));
h=@(t) heaviside(12.*t.*exp(55*t));
xf=vpa(fourier(f,t,w));
hf=vpa(fourier(g,t,w));
fplot(@(w) abs(hf), [-20*pi 20*pi])
The answer I was leading you to is to plot abs(xf) instead of abs(hf)
Which function exactly do you want to plot? As Walter mentioned below, you can't plot the idea of a Fourier transform. I expect you want to plot the Fourier transform of either x or h. You don't actually use either in your current code.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

fplot(abs(hf), [-20*pi 20*pi])

14 Kommentare

Now deleted comment by Mehmet Güneri:
Hi! Thank you sir but it couldnt solve my problem I still get the same error.
syms f(t) g(t) w;
x=@(t)heaviside(exp(-20*t));
h=@(t) heaviside(12.*t.*exp(55*t));
xf=vpa(fourier(f,t,w));
hf=vpa(fourier(g,t,w));
fplot(abs(hf), [-20*pi 20*pi])
Well, hf is the fourier transform of the undefined function g . You cannot just plot the idea of a fourier transform.
Thank you for your response but what can i do to plot that?
Nothing. You cannot illustrate the general idea of fourier transformation without using a series of plots. I recommend that you look at Wikipedia or some textbooks to see how they illustrate the general idea of fourier transform of unknown functions.
In the meantime, I would like to ask you why you go through the trouble to calculate xf but do not display the results and do not plot it? Why do you suddenly turn around and want to plot the fourier transform of the unknown function g?
Oh, right, you did not define f either.
You can potentially plot the fourier transform of x or h, but not of the undefined functions f or g.
sympref('HeavisideAtOrigin', 1)
ans = 
syms t w real
x=@(t)heaviside(exp(-20*t));
h=@(t) heaviside(12.*t.*exp(55*t));
f = fourier(x, t, w)
f = 
af = abs(f)
af = 
W = (-20:20)*pi;
AF = subs(af, w, W)
AF = 
try
plot(W, AF)
catch
fprintf('Ooops, abs(f) cannot be plotted!');
end
g = fourier(h, t, w)
g = 
ag = abs(g)
ag = 
AG = subs(ag, w, W)
AG = 
try
plot(W, AG)
catch
fprintf('Ooops, abs(g) cannot be plotted!');
end
Ooops, abs(g) cannot be plotted!
What went wrong? Well, it turns out that the Fourier transform of the Heaviside function is tricky to derive ( https://www.cs.uaf.edu/~bueler/M611heaviside.pdf ) and MATLAB simply doesn't have it registered in its table of functions. The transform does exist; see https://www.wolframalpha.com/input/?i=Fourier%5BHeaviside%5B12*t*Exp%5B55*t%5D%5D%2Ct%2Cw%5D -- though I notice that if I adjust the constant multipliers that Wolfram Alpha always gives the same result, and I am not positive that is correct.
Hello, I am getting the same error when I run the below code. Can someone kindly assist.
syms x y(x)
y0=1; x0=0;
y1=x-y^2; y2=diff(y1); y3=diff(y2); y4=diff(y3);
subs(y1,x,0); subs(y2,x,0); subs(y3,x,0); subs(y4,x,0);
subs(y1,y,1); subs(y2,y,1); subs(y3,y,1); subs(y4,y,1);
y=y0 + y1*(x-x0) + y2*(x-x0)^2/factorial(2) + y3*(x-x0)^3/factorial(3) + y4*(x-x0)^4/factorial(4);
fplot(y); xlim([-4 4]);
Warning: Error updating FunctionLine.

The following error was reported evaluating the function in FunctionLine update: Unable to convert expression containing remaining symbolic function calls into double array. Argument must be expression that evaluates to number.
I don't understand what you try to do in the code. A Taylor series in two dimensions of z(x,y) = x-y^2 ? But why do you define y as a function of x then ?
Thank you @Torsten.
Initially, I had the below code which was throwing the below error.
'Error using fplot>singleFplot
Input must be a function or functions of a single variable.'
syms x y
y0=1; x0=0;
y1=x-y^2; y2=diff(y1); y3=diff(y2); y4=diff(y3);
subs(y1,x,0); subs(y2,x,0); subs(y3,x,0); subs(y4,x,0);
subs(y1,y,1); subs(y2,y,1); subs(y3,y,1); subs(y4,y,1);
y=y0 + y1*(x-x0) + y2*(x-x0)^2/factorial(2) + y3*(x-x0)^3/factorial(3) + y4*(x-x0)^4/factorial(4);
fplot(y); xlim([-4 4]);
Error using fplot>singleFplot (line 258)
Input must be a function or functions of a single variable.

Error in fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 218)
hObj = cellfun(@(f) singleFplot(cax,{f},limits,extraOpts,args),fn{1},'UniformOutput',false);

Error in fplot>vectorizeFplot (line 218)
hObj = cellfun(@(f) singleFplot(cax,{f},limits,extraOpts,args),fn{1},'UniformOutput',false);

Error in fplot (line 184)
hObj = vectorizeFplot(cax,fn,limits,extraOpts,args);
As said: I don't know what you try to do.
Maybe a 2d-Taylor expansion around (0,1):
syms x y
y0=1; x0=0;
f=x-y^2;
f = 
g = subs(f,[x,y],[x0,y0])+...
subs(diff(f,x),[x,y],[x0,y0])*(x-x0) + ...
subs(diff(f,y),[x,y],[x0,y0])*(y-y0) + ...
subs(diff(f,x,2),[x,y],[x0,y0])*(x-x0)^2/2 + ...
subs(diff(diff(f,x),y),[x,y],[x0,y0])*(x-x0)*(y-y0) + ...
subs(diff(f,y,2),[x,y],[x0,y0])*(y-y0)^2/2;
g = simplify(g)
g = 
fsurf(g)
Thanks @Torsten
Actually I am trying to solve the below ode using the Taylor method:
dydx=x-y^2,
given y(x0)=1; x0=0;
I don't know how to convert yorder1,...,yorder5 into usual symbolic expressions instead of symbolic functions of x. Maybe you have an idea ...
syms x y(x)
x0 = 0;
y0 = 1;
f = x-y^2;
y1 = f; % First derivative
y2 = diff(y1,x);
y2 = subs(y2,diff(y,x),y1); % Second derivative
y3 = diff(y2,x);
y3 = subs(y3,diff(y,x),y1); % Third derivative
y4 = diff(y3,x);
y4 = subs(y4,diff(y,x),y1); % Fourth derivative
y5 = diff(y4,x);
y5 = subs(y5,diff(y,x),y1); % Fifth derivative
syms h
yorder1 = y0 + h*subs(y1,[x,y(x)],[x0,y0]);
yorder2 = yorder1 + h^2/2*subs(y2,[x,y(x)],[x0,y0]);
yorder3 = yorder2 + h^3/6*subs(y3,[x,y(x)],[x0,y0]);
yorder4 = yorder3 + h^4/24*subs(y4,[x,y(x)],[x0,y0]);
yorder5 = yorder4 + h^5/120*subs(y5,[x,y(x)],[x0,y0]);
subs(y1,[x,y(x)],[x0,y0])
ans(x) = 
subs(y2,[x,y(x)],[x0,y0])/2
ans(x) = 
subs(y3,[x,y(x)],[x0,y0])/6
ans(x) = 
subs(y4,[x,y(x)],[x0,y0])/24
ans(x) = 
subs(y5,[x,y(x)],[x0,y0])/120
ans(x) = 
eqn = diff(y,x)==f;
cond = y(x0)==y0;
y = dsolve(eqn,cond)
y = 
limit(y,x,0,"right")
ans = 
1
vpa(limit(diff(y,x),x,0,"right"))
ans = 
vpa(limit(diff(y,x,2),x,0,"right"))/2
ans = 
vpa(limit(diff(y,x,3),x,0,"right"))/6
ans = 
vpa(limit(diff(y,x,4),x,0,"right"))/24
ans = 
vpa(limit(diff(y,x,5),x,0,"right"))/120
ans = 
hold on
fplot(real(y),[0 1],'o')
fplot([yorder1(0),yorder2(0),yorder3(0),yorder4(0),yorder5(0)],[0 1])
hold off
grid on
Thank you very much @Torsten. I wouldn't have been able to crack it on my own.
If you use the Taylor method to solve ordinary differential equations, you won't approximate the solution only once for the initial conditions and take this polynomial as solution over the complete interval of integration as done above. You will use it for a (usually) low order and a small stepsize h to approximate the solution y in x0+h and proceed from there, taking x0 + h and y(x0+h) as new initial conditions.

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 20 Dez. 2020

Kommentiert:

am 24 Jul. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by