Calling function in the same script to plot graph

3 Ansichten (letzte 30 Tage)
Nur Adilah Ismail
Nur Adilah Ismail am 29 Dez. 2020
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');

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 29 Dez. 2020
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 Kommentare
Cris LaPierre
Cris LaPierre am 30 Dez. 2020
Assign the result to a variable.
z=Fun(in)
For more, see Ch 7 of MATLAB Onramp.
Nur Adilah Ismail
Nur Adilah Ismail am 1 Jan. 2021
Alright. Thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by