How to write a function in MATLAB?

조회 수: 2 (최근 30일)
mariam muner
mariam muner 2022년 2월 11일
답변: Walter Roberson 2022년 2월 11일
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