# How to vectorize the function?

5 views (last 30 days)
Xizeng Feng on 28 Nov 2021
Commented: Xizeng Feng on 28 Nov 2021
I run the following codes and had the result of fplot. But comes also some warning as in the bottom,
Can anyone tell me how to improve my codes, or how to vectorize my function?
fc=1e+8; % carrier
fm=1e+3; % modulate
fd=7.5e+4; % frequency deviation
r=0.25; t0=0.0005; % reflection and delay
w0=2*pi*fc; fm=2*pi*fm; % changing to angular frequenvy
out=@(t)(fd*sin(fm*t)-fd*sin(fm*(t-t0)))...
*(r^2+r*(cos(w0*t0+fd*cos(fm*t)/fm-fd*cos(fm*(t-t0))/fm)))...
/(1+r^2+2*r*cos((w0*t0+fd*cos(fm*t)/fm-fd*cos(fm*(t-t0))/fm)))+fd*sin(fm*t);
fplot(out,[0 .003])
------------------------------
the warnings:
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 237)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 192)
In fplot>vectorizeFplot (line 192)
In fplot (line 162)
In test1 (line 10)

per isakson on 28 Nov 2021
Edited: per isakson on 28 Nov 2021
Read Array vs. Matrix Operations. With scalars the two produce the same result, but not with vectors and arrays.
The second section of the script is the vectorize version.
%%
fc=1e+8; % carrier
fm=1e+3; % modulate
fd=7.5e+4; % frequency deviation
r=0.25; t0=0.0005; % reflection and delay
w0=2*pi*fc; fm=2*pi*fm; % changing to angular frequenvy
%%
t = linspace( 0, 0.003, 1000 );
y = (fd.*sin(fm.*t)-fd.*sin(fm.*(t-t0))) ...
.*(r^2+r.*(cos(w0.*t0+fd.*cos(fm.*t)./fm-fd.*cos(fm.*(t-t0))./fm))) ...
./(1+r^2+2.*r.*cos((w0.*t0+fd.*cos(fm.*t)./fm-fd.*cos(fm.*(t-t0))./fm)))+fd.*sin(fm.*t);
figure
plot( t, y )

Chunru on 28 Nov 2021
fc=1e+8; % carrier
fm=1e+3; % modulate
fd=7.5e+4; % frequency deviation
r=0.25; t0=0.0005; % reflection and delay
w0=2*pi*fc; fm=2*pi*fm; % changing to angular frequenvy
% Change * and / into .* and ./
out=@(t) (fd*sin(fm*t)-fd*sin(fm*(t-t0)))...
.*(r^2+r*(cos(w0*t0+fd*cos(fm*t)/fm-fd*cos(fm*(t-t0))/fm)))...
./(1+r^2+2*r*cos((w0*t0+fd*cos(fm*t)/fm-fd*cos(fm*(t-t0))/fm)))+fd*sin(fm*t);
fplot(out,[0 .003])
Xizeng Feng on 28 Nov 2021

### Categories

Find more on Graphics Object Programming in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by