code will not run, but gives no error message

53 views (last 30 days)
I have written the following code that involves multiple functions. S calls A and B. Evidently this is wrong because the code is not running. However, I do not know what the problem is because it detects nothing wrong and gives no error message. I know that I can just define one function to complete this task, but I would like to know the best way to call a function within another function in matlab.
Here is the code:
function S = entropy(r,n)
A = stirling1(r,n);
B = stirling2(n);
S = A-log(factorial(r))-B;
function A = stirling1(r,n)
A = (r+n-1)*log(r+n-1)-(r+n-1)+log(2*pi(r+n-1));
disp(S)
end
function B = stirling2(n)
B = (n-1)*log(n-1)-(n-1)+log(2*pi*(n-1));
disp(S)
end
fprintf('entropy =%9.6\n',entropy(50,250))
end

Accepted Answer

Steven Lord
Steven Lord on 17 Feb 2021
Let's say you call your entropy function with inputs 1 and 2.
entropy(1, 2)
On the second line of your entropy function, it calls stirling1 with inputs 1 and 2. This does some calculations then tries to display the contents of the variable S. This should probably throw an error as you haven't created the variable S yet. The only variables that exist in the workspace at the time that disp statement executes are r, n, and A.
But let's say that works. Then your code calls stirling2 with input 2. That does some calculations then displays S (which still hasn't been created) again.
Now on the next line you create a variable S.
The final line of your entropy function calls the entropy function with inputs 50 and 250.
This calls stirling1 with inputs 50 and 250, then calls stirling2 with input 250, then computes S, and finally calls the entropy function with inputs 50 and 250.
This calls stirling1 with inputs 50 and 250, then calls stirling2 with input 250, then computes S, and finally calls the entropy function with inputs 50 and 250.
This calls stirling1 with inputs 50 and 250, then calls stirling2 with input 250, then computes S, and finally calls the entropy function with inputs 50 and 250.
This calls stirling1 with inputs 50 and 250, then calls stirling2 with input 250, then computes S, and finally calls the entropy function with inputs 50 and 250.
...
Eventually MATLAB should throw an error about the recursion limit being reached (entropy calling itself too many times.) Or if you've increased the recursion limit MATLAB and/or your computer could crash.
I would not try to display S before it has been computed (get rid of the disp statements in stirling1 and stirling2) and I also wouldn't call entropy from within itself (at least not without some sort of base case that can stop the recursive calls.) Type that entropy(50, 250) call in the Command Window, or make this a script file (where the script calls entropy(50, 250) and that calls the local entropy function), or create a separate script file that just calls entropy(50, 250).

More Answers (1)

Eleftherios
Eleftherios on 7 Dec 2022
function dxdt = odefun (t,x)
dxdt = zeros(3,1);
dxdt(1)= -(8/3)*x(1)+x(2)*x(3);
dxdt(2)= -10*x(2)+10*x(3);
dxdt(3)= -x(3) -x(2)*x(1)+28*x(2);
end
'Matlab says the problem is at the (t) , what can i do?

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!