How to write a function in MATLAB?

7 vues (au cours des 30 derniers jours)
mariam muner
mariam muner le 11 Fév 2022
Hello
i need to write this function using matlab
where theta takes the range of values ( 0 to 80 step 1) and the frequncy (f) take the values (18 to 22 ) and the other parameters are fixed
then i need to plot the result with the values of frequency (x-axis include the frequency and y-axis include the G)
i tried to execute it for one values of (theta and f) using this code
gain_mag=sin(N*pi/2*sin(theta)*(freq/fc-1))/sqrt(N)*sin(pi/2*sin(theta)*(freq/fc-1));
gain_phase=exp(j*0.5*(N-1)*pi*sin(theta)*(freq/fc-1));
gain=gain_mag*gain_phase;
plot(gain);
grid on;
how can i execute it for different values of theta and f?

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Fév 2022
theta = (0:80).'; %column vector
freq = 18:22; %row vector
gain_mag = sin(pi/2 .* N .* sin(theta) .* (freq/fc-1)) ./ sqrt(N) .* sin(pi/2 .* sin(theta) .* (freq/fc-1));
gain_phase = exp(1j*pi/2 .* (N-1) .* sin(theta) .* (freq/fc-1));
gain = gain_mag .* gain_phase;
surf(freq, theta, gain)
There are two tricks here:
  1. Use element-by-element operators such as .* because you are going to be multiplying and dividing non-scalars
  2. Use a row vector for one of the variables and a column vector for the other one, and starting R2015b MATLAB will automatically expand that into grids of calculations. If you are using an earlier version, you would want to use meshgrid() to make theta and freq 2D grids of values.

Plus de réponses (0)

Catégories

En savoir plus sur Numeric Types 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