How to write a function in MATLAB?

2 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2022-2-11
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by