필터 지우기
필터 지우기

Plotting the objective function

조회 수: 12 (최근 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에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by