Clear Filters
Clear Filters

how to call a sub function in the main function

3 views (last 30 days)
suppose my program is like tis
function z=prog1
Lower_1 =0;
Upper_1 = 10;
Lower_2 =0;
Upper_2 = 10;
for i = 1: times
X (i, 1) = (Lower_1 + (Upper_1-Lower_1) );
X (i, 2) = (Lower_2 + (Upper_2-Lower_2) );
T (i) = f(X(i,1),X(i,2)) ;
subplot(2,2,1)
plot(X(i,1),X(i,2),'k.');
hold on;
grid on;
end
function [F]=f(x1,x2)
x1=0:0.1:10;
f=gaussmf(x1,[2 5])/x2;
plot(x1,x2);
xlabel('gaussmf, P=[2 5]');
end
in this program when am running the second function 'function [F]=f(x1,x2) ' independently ,its working nice and showing output. but when am calling in the main program its showing some errors... please make me understand how i have to clear the error and why the error is comming...please try to debugg the program
thanks in advance

Answers (2)

Honglei Chen
Honglei Chen on 31 Jan 2012
Your output, F, is never defined in f() function. In addition, you are rewriting your input x1 in that function too.

Walter Roberson
Walter Roberson on 31 Jan 2012
What errors are showing up?
Why do you bother to pass the first parameter in to f, as you are going to overwrite x1 inside your routine?
(Lower_1 + (Upper_1-Lower_1) ) is the same as just Upper_1 so it is not clear why you would bother with the calculation ?
Why is your loop always calculating the same values each time?
You have a plot() inside the f() call. That is going to plot against the current axis. After your subplot() command in your loop, the current axis is going to be the axis of the second subplot, and that is not going to change afterwards since you have no axes() or other subplot() commands to change the current axes to something else. Is that what you want, that the first plot will go somewhere and all the rest of the plots will go to subplot(2,2,1) ??

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!