MATLAB Answers

i want to know whats is the problem in this code during running

3 views (last 30 days)
Mohamed Ayman
Mohamed Ayman on 27 Nov 2019
Commented: Image Analyst on 27 Nov 2019
function saida = hatacost231(distance, frequency, ht, hr, zone)
% saida = hatacost231(distancia, frequencia, ht, hr, zona)
%
% saida : Vector with loss estimates as a function of distance to a fixed frequency value
% distance : Vector com valores da distancia expressa em km
% Recomenda-se que seja superior a 1 km
% frequency: Frequencia de trabalho, expressa em MHz,
% Recomenda-se que se situe na gama [150,2000] MHz
% ht : Effective height of transmitting antenna (in meters)
% It is recommended to be in the range [30, 200] (meters)
% hr : Effective antenna height at mobile terminal (in meters) It is recommended to be in the range [1, 10] (meters)
% zone : Classification of the area under analysis for the alphacorrection parameter (Hre)
% 1 - Cities with little and medium degree of urbanization
% 2 - Major cities
% 3 - Areas suburbanas
% 4 - THE SITUATION FOR RURAL AREAS NOT PROVIDED.
distance=1;
ht=100;
hr=10;
Zone=2;
frequency=1900;
if frequency > 1500
for k = 1 : length(distance),
loss = 46.30 + 33.90*log10(frequency) - 13.82*log10(ht) +(44.9 - 6.55*log10(ht))*log10(distance(k));
loss = loss -(1.1*log10(frequency)-0.7)*hre +(1.56*log10(frequency)-0.8);
if zona ~= 2
loss = loss + 3;
end
saida(k) = loss;
end
else
for k = 1 : length(distance),
loss = 69.55 + 26.16*log10(frequency) - 13.82*log10(ht) +(44.9-6.55*log10(ht))*log10(distance(k));
saida(k) = loss -(1.1*log10(frequency)-0.7)*hr +(1.56*log10(frequency)-0.8);
end
end

  2 Comments

Walter Roberson
Walter Roberson on 27 Nov 2019
What error message are you encountering when you run the code?
function saida = hatacost231(distance, frequency, ht, hr, zone)
but then
distance=1;
ht=100;
hr=10;
Zone=2;
frequency=1900;
Why do you allow variables to be passed into the function if you are just going to overwrite the values?
Mohamed Ayman
Mohamed Ayman on 27 Nov 2019
function saida = hatacost231(distance, frequency, ht, hr, zone)
|
Error: Function definitions are not permitted in this context.

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 27 Nov 2019
Edited: the cyclist on 27 Nov 2019
Are you trying to just paste that code into the command line, and running it?
Instead, you should save that function code into a file named hatacost231.m. Then call that function from the command window, for example.

  5 Comments

Show 2 older comments
Mohamed Ayman
Mohamed Ayman on 27 Nov 2019
give me this error
Attempt to execute SCRIPT hatacost231 as a function:
C:\Users\elgaml\Desktop\hatacost231.m
the cyclist
the cyclist on 27 Nov 2019
OK, just in case I wasn't clear, let's test a very simple example. Make a file named hatatest.m, with the following contents:
% =============================
function out = hatatest(in1,in2)
out = in1 + in2;
end
% =============================
and the way you should call it from the command window would be
>> output = hatatest(5,6)
which would give the answer
output =
11
Image Analyst
Image Analyst on 27 Nov 2019
Does your function m-file have ANYTHING in front of the "function" line? It can have only comments. If you want to run it and define the function in the same file, that's okay after a certain MATLAB version but the functions must end with an "end" statement, which yours does not.
Try this (all in one m-file called whatever you want.) I've also attach an m-file.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define parameter values to pass in.
distance=1;
ht=100;
hr=10;
zone=2;
frequency=1900;
% Call the function.
saida = hatacost231(distance, frequency, ht, hr, zone)
% end of script portion of the m-file.
%========================================================================================
function saida = hatacost231(distance, frequency, ht, hr, zone)
% saida = hatacost231(distancia, frequencia, ht, hr, zona)
%
% saida : Vector with loss estimates as a function of distance to a fixed frequency value
% distance : Vector com valores da distancia expressa em km
% Recomenda-se que seja superior a 1 km
% frequency: Frequencia de trabalho, expressa em MHz,
% Recomenda-se que se situe na gama [150,2000] MHz
% ht : Effective height of transmitting antenna (in meters)
% It is recommended to be in the range [30, 200] (meters)
% hr : Effective antenna height at mobile terminal (in meters) It is recommended to be in the range [1, 10] (meters)
% zone : Classification of the area under analysis for the alphacorrection parameter (Hre)
% 1 - Cities with little and medium degree of urbanization
% 2 - Major cities
% 3 - Areas suburbanas
% 4 - THE SITUATION FOR RURAL AREAS NOT PROVIDED.
if frequency > 1500
for k = 1 : length(distance)
loss = 46.30 + 33.90*log10(frequency) - 13.82*log10(ht) +(44.9 - 6.55*log10(ht))*log10(distance(k));
loss = loss -(1.1*log10(frequency)-0.7)*hr +(1.56*log10(frequency)-0.8);
if zone ~= 2
loss = loss + 3;
end
saida(k) = loss;
end
else
for k = 1 : length(distance)
loss = 69.55 + 26.16*log10(frequency) - 13.82*log10(ht) +(44.9-6.55*log10(ht))*log10(distance(k));
saida(k) = loss -(1.1*log10(frequency)-0.7)*hr +(1.56*log10(frequency)-0.8);
end
end
end % of function hatacost231()

Sign in to comment.

Sign in to answer this question.