# How to vectorize the function?

3 vues (au cours des 30 derniers jours)
Xizeng Feng le 28 Nov 2021
Commenté : Xizeng Feng le 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)
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

per isakson le 28 Nov 2021
Modifié(e) : per isakson le 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 )
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Plus de réponses (1)

Chunru le 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])
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Xizeng Feng le 28 Nov 2021
thank you very much for your answer!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by