Calling function in the same script to plot graph

조회 수: 3 (최근 30일)
Nur Adilah Ismail
Nur Adilah Ismail 2020년 12월 29일
댓글: Nur Adilah Ismail 2021년 1월 1일
I have this function
function z=Fun(u)
d=length(u);
a1=1000
b1= 16.19
c1=0.00048
a2=700
b2=16.6
c2=0.002
a3=680
b3=16.5
c3=0.00211
a4=370
b4=22.26
c4=0.0072
a5=660
b5=25.92
c5=0.00413
p1max=455
p2max=130
p3max=130
p4max=80
p5max=55
pd=480
p1=u(1)
p2=0
p3=0
p4=0
p5=pd-p1
z=a1 + (b1*p1) + c1*(p1)^2 + a5 + (b5*p5) + c5*(p5)^2 +60
I would like to call the z value in another function to plot graph z vs iteration
plot(N_iter,z); %N_iter is the number of iteration
xlabel('Iteration');
ylabel('Cost');

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 12월 29일
You would need to first call the function and capture its output in a variable named z.
in = ???;
z = Fun(in);
plot(N_iter,z); %N_iter is the number of iteration
xlabel('Iteration');
ylabel('Cost');
Learn more about functions here.
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2020년 12월 30일
Assign the result to a variable.
z=Fun(in)
For more, see Ch 7 of MATLAB Onramp.
Nur Adilah Ismail
Nur Adilah Ismail 2021년 1월 1일
Alright. Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by