Advice using function titles with abs command and plotting with function command

Hi all, continuing my code for a fourier series assignment and have hit a brick wall surrounding the function handles and the abs command. Since my last question i have changed the layout of the code and created a single function from the piecewise function i originally had. However, adding this in seperately it was plotting fine (apart from a few data cursor errors when moving the plot(error updating pointdatatip), but then adding the second fplot line stops the first from plotting. Additionally, the errors i am recieving are exclusively related to the first fplot line.
My code is below:
clear
ft=@f;
w=2;
T=(2*pi)/w;
hold on
fplot(ft,[-pi,pi])
%evaluating C0
c0=(1/T)*integral(ft,-T/2,0)+(1/T)*integral(ft,0,T/2);
%evaluating Cn values up to n=10 from n=1
for n = 1:10
f3=@(t) ((4+t)/2).*exp(-1j*n*w*t);
f4=@(t) ((2-t).*cos(2*t)).*exp(-1j*n*w*t);
c(n,1)=(1/T)*integral(f3,-T/2,0)+(1/T)*integral(f4,0,T/2);
end
%turning values n=1:10 to conjugates
cc=conj(c);
%series addition for k=-10:10
k=(1:10).';
s=@(t) c0+sum(c(k).*exp(1j*k*w*t),1)+sum(cc(k).*exp(1j*-1*k*w*t),1);
fplot(s,[-pi,pi],'r')
%evaluating the average energy of the signal
f5=@(t) abs((4+t)/2).^2;
f6=@(t) abs((2-t).*cos(2*t)).^2;
E=(1/T)*integral(f5,-T/2,0)+(1/T)*integral(f6,0,T/2);
Above and below this point i would prefer to use the abs(ft).^2 above, however, this works just fine
I am not sure how to implement the line abs(ft-s) below, as they might need scalar values or arrays?
z1=0;
z2=0;
x=0;
while z2<1/10
s2=@(t) c0+sum(c(x).*exp(1j*x*w*t),1)+sum(cc(x).*exp(1j*-1*x*w*t),1);
x=+1;
z1=ft-s2;
z2=abs(z1);
end
function y=f(t)
if (-pi<t)&(t<0)
y=(4+t)/2;
elseif (0<=t)&(t<pi)
y=(2-t).*cos(2*t);
end
end
Thanks in advance for any help given

4 comentarios

Jan
Jan el 25 de Mzo. de 2021
Editada: Jan el 25 de Mzo. de 2021
Please explain "it will not work" with any details.
Is it intented, that y is undefined in f(), when it is < -pi or > pi?
Yes, so y being the output, the interval for the given equation is [-pi,pi]
edit: so i just changed it so y is defined for <-pi and >pi and now both plot on the same graph. However, now this error occurs:
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.
Jan
Jan el 25 de Mzo. de 2021
Editada: Jan el 25 de Mzo. de 2021
Where does this warning occur?
It is still not clear to me, what your problem is.
The warning is exclusive to the first fplot line and im not sure how to fix it

Iniciar sesión para comentar.

Respuestas (2)

s2=@(t) c0+sum(c(x).*exp(1j*x*w*t),1)+sum(cc(x).*exp(1j*-1*x*w*t),1);
x=+1;
z1=ft-s2;
s2 is a function handle. You need to invoke it with a parameter.
x=+1;
You are assigning positive 1 to x, not adding 1 to x.

3 comentarios

Thank you, i have changed the x=+1; to x=x+1.
ft and s2 are both function handles in this scenario, so a better question from me would be how i subtract two function handles to give me a scalar which i can then apply the absolute command to to fit my while loop. If it can be done at all?
No, you cannot take the difference between function handles. A function handle is a pointer to a block of memory where the struct() is that holds the information about the function handle. If there were a meaning for subtraction it would mean how far apart the data structures are in memory.
You can invoke a function handle on a value to get a numeric result. However, you would need a definite t to invoke the handle on, and based on what you have posted, you do not have a definite t.
Okay thank you, thats what i was wondering, is there anyway to overcome this or would i have to re-write my code in another way with a definite value for t?

Iniciar sesión para comentar.

If f the values t == pi and t == -pi are excluded also. Is this wanted?
Do you want f to accept vectors for t? Then:
function y = f(t)
y = zeros(size(t));
index = (-pi<t) & (t<0);
y(index) = (4 + t(index)) / 2;
index = (0<=t) & (t<pi)
y(index) = (2 - t) .* cos(2 * t);
end
The IF condition must be a scalar, so if pi<t is converted implicitly to if all(pi < t).

2 comentarios

Accepting scalars for what i am trying to do throughout this code is fine. Implementing this code however, still gives the error telling me that the function behaves unexpectedly
Jan
Jan el 25 de Mzo. de 2021
Editada: Jan el 25 de Mzo. de 2021
@Jai Harnas: Please do not rephrase the error message in your word, but post a copy of the complete message. Then it woulöd start to get clear, in which part of the code the problem is.
"Accepting scalars for what i am trying to do throughout this code is fine." - I do niot think so. You get a corresponding error message. In you original code you provide the interval [-pi, pi], but for t==pi and t==-pi the function f does not reply an output.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 25 de Mzo. de 2021

Editada:

Jan
el 25 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by