Plotting the objective function

7 次查看(过去 30 天)
Vivek
Vivek 2023-2-3
回答: Sarthak 2023-3-7
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.
function P1=f(x0)
M=5;
x0=[5,8];
% x0=[12,13];
M2=m(M,x0(1));
M3=m(M2,x0(2));
M4=m(M3,(x0(1)+x0(2)));
beta1=b(M,(x0(1)));
beta2=b(M2,(x0(2)));
beta3=b(M3,((x0(1)+x0(2))));
s1=sin(beta1);
s2=sin(beta2);
s3=sin(beta3);
t1n=(13.824)*((M2*M3*M)^2)*((s1*s2*s3)^2);
t1d=(((0.4)*(M3^2)*(s3^2))+2)*(((0.4)*(M2^2)*(s2^2))+2)*(((0.4)*(M^2)*(s1^2))+2);
t1=(t1n/t1d)^(3.5);
t2n=13.824;
t2d=((2.8*(M3^2)*(s3^2))-0.4)*((2.8*(M2^2)*(s2^2))-0.4)*((2.8*(M^2)*(s1^2))-0.4);
t2=(t2n/t2d)^(2.5);
P1=(t1*t2);
P= P1*(-1);
% surf(
% fplot(x0,f)
end
  5 个评论
Vivek
Vivek 2023-2-5
But the objective function is f(x0) and it is scripted in different file and the function has to be returned while plotting.
The below graph is returned by using Z = arrayfun(@(x,y) f([x,y]),X,Y);
Torsten
Torsten 2023-2-5
编辑:Torsten 2023-2-5
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.

请先登录,再进行评论。

回答(1 个)

Sarthak
Sarthak 2023-3-7
Hi,
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.

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by