Plotting the objective function
6 views (last 30 days)
Show older comments
The below shown objective function has to be plotted and while doing so its going in infinite recursion.
The functions m and b are external functions defined in different function files.
Torsten on 5 Feb 2023
Edited: Torsten on 5 Feb 2023
Your objective function accepts a vector with two elements [x y].
x1 = linspace(1,100,55);
x2 = linspace(1,100,55);
[X, Y] = meshgrid(x1, x2);
Z = arrayfun(@(x,y) f([x,y]),X,Y);
This code passes all tuples (x1(i),y1(j)) to your function f, evaluates f at these points and saves the result in Z(i,j).
If the subsequent command
surfc(X, Y, Z)
gives you a surface with constant z value, your function f does not seem to behave properly.
Sarthak on 7 Mar 2023
It looks like the function ‘f(x0)’ is not recursively calling itself, but it might be stuck in an infinite loop due to some other reason. One possible reason could be that the external functions ‘m’ and ‘b’ are calling back the ‘f(x0)’ function, which can lead to an infinite recursion. Another reason could be that the values of ‘M’, ‘x0(1)’, and ‘x0(2)’ are not changing during the execution, leading to a loop that never terminates.
To debug this issue, you can try printing the values of the variables ‘M’, ‘x0(1)’, and ‘x0(2)’ at different points in the function to see if they are changing as expected. You can also try commenting out parts of the code to see which part is causing the infinite loop.
Once you have identified the issue, you can modify the code accordingly to fix the problem.
Find more on Surface and Mesh 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!