I tried to call a function but it failed
이전 댓글 표시
When i tried to return a value from the function (rate) to main file it failed and it shows undefined variable R_tow.
%main file
clc
clear all
close all
T=1;
tow_min=0;
tow_max=T;
tow=0.5
while (tow_max-tow_min)>0.02
tow_bar=(tow_max+tow_min)/2;
if crn(tow_bar)==crn(tow_min)
tow_min=tow_bar;
else
tow_max=tow_bar;
end
end
tow0=tow_bar;
disp("tow0="+tow0);
rate(tow0);
("R_tow="+R_tow);
plot(R_tow,tow0);
function R_tow=rate(tow)
PH0=0.1
Pa=10;
Q=20;
y=0.1;
N=3;
yi=[1,3,5];
Pdbar=0.2;
Pi=[3,4,2];
alpha=sqrt((2*y)+1)*qfuncinv(Pdbar);
beta=0
for i =2:N
x=1+((Pi(i)*yi(i))/((1+sum(Pi(i)*yi(3:N)))));
beta=beta+log2(x);
end
fs=100000;
z=1+sum(Pi(2:N).*yi(2:N));
S=(Pa*yi(1))/z;
x1=(1-(qfunc(alpha+(sqrt(tow*fs)*y))))
y1=log2(1+(((tow*Pa/1-tow)-sum(Pi(2:N)))*yi(1))/z)
R_tow=PH0*(1-tow)*x1*y1+beta;
%disp(R_tow)
end
댓글 수: 2
Navid Zolfaghari Moheb
2020년 2월 28일
So change the last four line of your code to:
disp("tow0="+tow0);
R_tow = rate(tow0);
("R_tow="+R_tow);
plot(R_tow,tow0);
you have a function that returns a value and you call that value "R_tow". But this is local only to that function. Now if you want to use it in another function you have to tell that function to get the output from my 'rate' function and call it "R_tow". Having it defined as "R_tow = rate(tow)" in function does not mean that whenever I run rate(tow), I will have an output called R_tow. It means that it has an output but you have ot name it. For example, in your first function, you could have "y=rate(tow)" or "zeta = rate(tow)" or in this case "R_tow=rate(tow)".
Barry Allen
2020년 3월 3일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!