How to find the minimum of a function and plot with it

2 次查看(过去 30 天)
My mission is to find Mycp such that Jcp is minimum and then plot a graph with minimum of Mycp as a function of rho.
I just started using matlab so dont understand why the graph is not showing? Can someone please help me?
close all
L=1;
T=100;
r=0.03;
I1=0.1;
p=0.05;
epsilon=0.3;
beta=0.1;
rho=1000;
Myrho=0:100:1000
MycpRho=zeros(numel(Myrho),1);
for j = 1:numel(Myrho)
MyrhoCurrent=Myrho(j);
Mycp = 0:1:100
n = zeros(numel(Mycp),1 );
n2 = zeros(numel(Mycp),1 );
n3 = zeros(numel(Mycp),1 );
Jcp = zeros(numel(Mycp),1 );
for i = 1:numel(Mycp )
MycpCurrent=Mycp(i);
delta = 1-MycpCurrent/100 ;
tau = (1/(beta*(L+delta*p)))*log((L*(I1+delta*p))/(delta*p*(L-I1 )));
t05 =(1/(beta*(L+delta*p)))*log((L*(0.05*L+delta*p))/(delta*p*(L-0.05*L )));
I2= @(t)(L*delta*p*(exp (beta*(L+delta*p)*t)-1)) ./ (L + delta*p* exp(beta*(L+delta*p)*t ));
I3= @(t)(L*(I1+delta*p)*exp((epsilon*beta)*(L+delta*p)*(t-tau))-...
delta*p*(L -I1))./(L-I1+(I1+delta*p)*exp(epsilon*beta*(L+delta*p)*(t-tau)));
fun = @(t,MycpCurrent) MycpCurrent*L*exp(-r*t);
fun2=@(t)rho*I2(t).*exp(-r*t);
fun3=@(t)rho*I3(t).*exp(-r*t);
n(i) = integral(@(t)fun(t,MycpCurrent),0,100, 'ArrayValued',1);
n2(i)= integral(fun2,t05,tau);
n3(i)= integral(fun3,tau,100);
JCp(i)= n(i)+n2(i)+n3(i);
end
MycpBest=Mycp(JCp==min(JCp));
JCpBest=min(JCp);
plot(Myrho,MycpBest)
hold on
scatter(MycpBest,JCpBest)
hold off
end

回答(1 个)

Charles DeLorenzo
Charles DeLorenzo 2019-7-10
I changed your plot to this and it seems to work fine. All the values seem to show up on the plot
JCpBest=min(JCp);
figure (1)
plot(Myrho,MycpBest,'x',MycpBest,JCpBest,'x');
However, i changed your second for loop to
for i = 1:numel(Mycp )-1
to match
Mycp = 0:1:100
or else the code gives a warning.
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by