Plotting the objective function

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

Torsten
Torsten 2023년 2월 3일
In the function f you supplied, I don't see a reason for an infinite recursion.
So we cannot give advice.
Vivek
Vivek 2023년 2월 3일
편집: Vivek 2023년 2월 3일
elow shown is my objective function and related m and b function files are external 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
%-----------------------------------------------------------------------
%-----------------------------------------------------------------------
%This is the separate script file
x0=[x1,x2];
% Lower bounds
lb=[1,5];
% Upper Bounds
ub=[40,38];
nonlcon=@area
%
rng default
x1 = linspace(1,100,55);
x2 = linspace(1,100,55);
[X, Y] = meshgrid(x1, x2);
Z = f(x0);
%contour(X, Y, Z)
surfc(X, Y, Z)
xlabel('x_1'), ylabel('x_2'), zlabel('f(x_1, x_2)')
% opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter');
% opts1 = optimoptions(opts,'MaxIterations',100); % Recommended
% [x,fval,exitflag,output]= fmincon(@f,x0,[],[],[],[],lb,ub,[],opts1);
options=optimoptions('ga','ConstraintTolerance',1e-8,'Display','iter');
[x,fval,exitflag,output,population,scores]=ga(@f,2,[],[],[],[],lb,ub,nonlcon,[],options)
After using the above code it is showing the error as shown below.
Ig it is taking single value of x0
Error using surfc
The surface Z must contain more than one row or column.
Error in z (line 18)
surfc(X, Y, Z)
Torsten
Torsten 2023년 2월 3일
편집: Torsten 2023년 2월 3일
Z = arrayfun(@(x,y) f([x,y]),X,Y);
instead of
Z = f(x0);
And my guess is that you mean
P1= P1*(-1);
instead of
P= P1*(-1);
at the end of function f.
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일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2023년 2월 3일

답변:

2023년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by