Hello my matlab code calls a cplexmiqp function to optimize a performance index.
The MATLAB code I am using calls a miqp function as from cplex
[v, fval, exitflag, output] = cplexmiqp(H, f, G, h, [], [], [], [], [], [], [], ctype );
I would be using the function "MIQPS_MATPOWER Mixed Integer Quadratic Program Solver for MATPOWER" from https://matpower.org/docs/ref/matpower6.0/miqps_cplex.html
I already have cplex optimization studio, but my question is where do I enter the inputs on the function?
For example using this inputs on the function:
H = [ 1003.1 4.3 6.3 5.9;
4.3 2.2 2.1 3.9;
6.3 2.1 3.5 4.8;
5.9 3.9 4.8 10 ];
c = zeros(4,1);
A = [ 1 1 1 1
0.17 0.11 0.10 0.18 ];
l = [1; 0.10];
u = [1; Inf]; xmin = zeros(4,1);
x0 = [1; 0; 0; 1];
opt = struct('verbose', 2);
[x, f, s, out, lambda] = miqps_cplex(H, c, A, l, u, xmin, [], x0, %%vtype, opt);
same question at
https://www.mathworks.com/matlabcentral/answers/1725610-using-cplex-with-matlab-newest-version/?s_tid=ans_lp_feed_leaf
The Matlab direct API in CPLEX was removed a few versions ago. And you can see more details in detailed requirements for cplex
Related
I am not very used to MATLAB and I'm trying to solve the following problem using MATLAB ode45, however, it's not working.
I was working on a problem in reaction engineering, using a Semi-Batch Reactor.
The reaction is given by
A + B ---> C + D
A is placed in the reactor and B is being continuously added into the reactor with a flowrate of v0 = 0.05 L/s. Initial volume is V0 = 5 L. The reaction is elementary. The reaction constant is k = 2.2 L/mol.s.
Initial Concentrations: for A: 0.05 M, for B: 0.025 M.
Performing a mole balance of each species in the reactor, I got the following 4 ODEs, and the expression of V (volume of the reactor is constantly increasing)
Solving this system and plotting the solution against time, I should get this
Note that plots of C(C) and C(D) are the same.
And let's set tau = v0/V.
Now for the MATLAB code part.
I have searched extensively online, and from what I've learned, I came up with the following code.
First, I wrote the code for the ODE system
function f = ODEsystem(t, y, tau, ra, y0)
f = zeros(4, 1);
f(1) = ra - tau*y(1);
f(2) = ra + tau*(y0(2) - y(2));
f(3) = -ra - tau*y(3);
f(4) = -ra - tau*y(4);
end
Then, in the command window,
t = [0:0.01:5];
v0 = 0.05;
V0 = 5;
k = 2.2;
V = V0 + v0*t;
tau = v0./V;
syms y(t);
ra = -k*y(1)*y(2);
y0 = [0.05 0.025 0 0];
[t, y] = ode45(#ODEsystem(t, y, tau, ra, y0), t, y0);
plot(t, y);
However, I get this...
Please if anyone could help me fix my code. This is really annoying :)
ra should not be passed as parameter but be computed inside the ODE system. V is likewise not a constant. Symbolic expressions should be used for formula transformations, not for numerical methods. One would also have to explicitly evaluate the symbolic expression at the wanted numerical values.
function f = ODEsystem(t, y, k, v0, V0, cB0)
f = zeros(4, 1);
ra = -k*y(1)*y(2);
tau = v0/(V0+t*v0);
f(1) = ra - tau*y(1);
f(2) = ra + tau*(cB0 - y(2));
f(3) = -ra - tau*y(3);
f(4) = -ra - tau*y(4);
end
Then use the time span of the graphic, start with all concentrations zero except for A, use the concentration B only for the inflow.
t = [0:1:500];
v0 = 0.05;
V0 = 5;
k = 2.2;
cB0 = 0.025;
y0 = [0.05 0 0 0];
[t, y] = ode45(#(t,y) ODEsystem(t, y, k, v0, V0, cB0), t, y0);
plot(t, y);
and get a good reproduction of the reference image
I am using Matlab to try and solve a system of three first-order ODEs, but the error message I get is 'syms' requires Symbolic Math Toolbox.
Error in spiders (line 1)
syms f(t) s(t) v(t) r W c h q a k b H K e
On a previous occasion, I received an error saying that this system of ODEs cannot be solved explicitly (i.e. in closed form). I think that numerical integration is the only way. The r,W,c,h, etc are parameters. Could someone please tell me how I can simulate/solve and plot the ODEs below?
syms f(t) s(t) v(t) r W c h q a k b H K e
r = 1;
W = 0.5;
c = 0.4;
h = 0.9;
q = 9;
a = 5;
k = 0.8;
b = 6;
H = 3;
K = 1.3;
e = 2;
ode1 = diff(f) == r*f*(1 - f/W) - c*s*f - h*(1 - q)*f;
ode2 = diff(s) == s*(-a + k*b*v/(H + v) + k*c*f) - h*K*q*s;
ode3 = diff(v) == v*(e - b*s/(H + v)) - h*q*v;
odes=[ode1;ode2;ode3]
S = dsolve(odes)
plot(S);
Looks like you where previously using a different MATLAB license, which included the Symbolic Math Toolbox. I assume you no longer have it available and you are now looking for numeric alternatives.
You have to define functions for the equations you are trying to solve. Then you can call one of the ode solvers. An example from the documentation:
y0 = [1; 0; 0];
tspan = [0 4*logspace(-6,6)];
M = [1 0 0; 0 1 0; 0 0 0];
options = odeset('Mass',M,'RelTol',1e-4,'AbsTol',[1e-6 1e-10 1e-6]);
[t,y] = ode15s(#robertsdae,tspan,y0,options);
Where robertsdae is the equation to solve. The full example including further explanations is available here.
I have a problem using the MATLAB DAE-solvers.
I'm trying to simulate the behaviour of a mechanical system using lagrangien mechanics. To do so, I followed the following tutorial to use MATLAB'sDAE-solvers.
But when I ran my code, I got the following error message:
Warning: Failure at t=5.076437e-01. Unable to meet integration tolerances without reducing the step size below the smallest value allowed
(1.803513e-15) at time t.
In ode15i (line 406)
Trying to find my mistake, I literally copied the code from the tutorial and ran it. The code is as follows:
syms l g m real
syms x(t) y(t) T(t)
eqns = [(m*diff(x(t),2) - T(t)/l*x(t)),
(m*diff(y(t),2) - T(t)/l*y(t) + m*g),
(x(t)^2 + y(t)^2 - l^2) ];
vars = [x(t); y(t); T(t)];
[eqns, vars] = reduceDifferentialOrder(eqns, vars);
if(~isLowIndexDAE(eqns, vars))
[DAEs, DAEvars] = reduceDAEIndex(eqns, vars);
[DAEs, DAEvars] = reduceRedundancies(DAEs, DAEvars);
end
%change to function, set parameters
f = daeFunction(DAEs, DAEvars, m, l, g);
m = 1.0;
r = 1.0;
g = 9.81;
F = #(t, Y, YP) f(t, Y, YP, m, r, g);
%get initial conditions
y0est = [0.5*r; -0.8*r; 0; 0; 0; 0; 0];
yp0est = zeros(7,1);
opt = odeset('RelTol', 10.0^(-7), 'AbsTol' , 10.0^(-7));
[y0, yp0] = decic(F, 0, y0est, [], yp0est, [], opt);
%simulate
[t,y] = ode15i(F, [0, 5], y0, yp0, opt);
I tried to solve a delay differential equation using dde23 but it seems I didn't understand it correctly so the function I wrote has error and I couldn't correct it to run to see if the output is correct.
I want to solve this system:
I couldn't understand how to add the last five equation to the program. I have to solve this system using table 1 parameter to gain an output such as fig 1 in image.
This is the code I wrote:
clear all;
clc;
lags=1;
sol=dde23(#eq24,lags,#eqh,[0 80]);
plot(sol.x,sol.y)
function v=eqh(t)
v=zeros(6,1);
function v=eq24(t,s,Ia,Is,R,N)
Alfa=0.1;
beta1=0.09;
beta2=0.1;
sigma1=0.3;
sigma2=0.4;
mu=0.01;
alfa=0.2;
rho=0.4;
r1=0.4;
r2=0.2;
d1=0.2;
d2=0.15;
k=0.1;
p=0.8;
tau=1;
T=4;
dsdt=Alfa-beta1*((s*Ia)/(1+sigma1*s))-beta2*((s*Is)/(1+sigma2*s))-mu*s+alfa*R;
dIadt=rho.*exp(-mu*tau).*s(((beta1.*Ia)/(1+sigma1.*s))+((beta2.*Is)/(1+sigma2.*s)))-(r1+d1+mu).*Ia;
dIsdt=(1-rho).*exp(-mu.*tau).*s(((beta1.*Ia)/(1+sigma1.*s))+((beta2.*Is)/(1+sigma2.*s)))+(1-k).*r1.*Ia-(r2+d2+mu).*Is;
dRdt=k.*r1.*Ia+r2.*Is-mu.*R-alfa.*R;
dNdt=Alfa-mu.*N-d1.*Ia-d2.*Is;
I have managed to come up with a running code. But I am still confused about t⁺ part of your question. The results of my code are almost the same as the ones you have presented. I hope this helps you with whatever you are doing.
function sol = eq242
clf
global lambda beta1 beta2 sigma1 sigma2 mu alpha rho r1 r2 d1 d2 k tau
lambda=0.1;beta1=0.09;beta2=0.1;sigma1=0.3;sigma2=0.4;mu=0.01;alpha=0.2;
rho=0.4;r1=0.4;r2=0.2;d1=0.2;d2=0.15;k=0.1;
opts = ddeset('RelTol',1e-5,'AbsTol',1e-8);
sol = dde23(#eq24,tau,[1, 1, 1, 1, 1],[0, 50],opts);
figure(1)
plot(sol(1).x,sol(1).y(1,:),sol(1).x,sol(1).y(2,:),sol(1).x,sol(1).y(3,:),sol(1).x,sol(1).y(4,:),sol(1).x,sol(1).y(5,:))
function dydt=eq24(t,y,Z)
global lambda beta1 beta2 sigma1 sigma2 mu alpha rho r1 r2 d1 d2 k tau
s = y(1);
Ia = y(2);
Is = y(3);
R = y(4);
N = y(5);
slag1 = Z(1,1);
ialag2 = Z(2,1);
islag3 = Z(3,1);
dsdt=lambda-beta1*((s*Ia)/(1+sigma1*s))-beta2*((s*Is)/(1+sigma2*s))-mu*s+alpha*R;
dIadt=rho*exp(-mu*tau)*slag1*(((beta1*ialag2)/(1+sigma1*slag1))+((beta2*islag3)/(1+sigma2*slag1)))-(r1+d1+mu)*Ia;
dIsdt=(1-rho)*exp(-mu*tau)*slag1*(((beta1*ialag2)/(1+sigma1*slag1))+((beta2*islag3)/(1+sigma2*slag1)))+(1-k)*r1*Ia-(r2+d2+mu)*Is;
dRdt=k*r1*Ia+r2*Is-mu*R-alpha*R;
dNdt=lambda-mu*N-d1*Ia-d2*Is;
dydt= [dsdt; dIadt; dIsdt; dRdt; dNdt];
How to minimise this function please teach me the code in MATLAB
Minimize Ra = (0.237 − 0.00175v + 8.693f − 0.00159z)
subjected to 124.53 ≤ v ≤ 167.03
0.025 ≤ f ≤ 0.083
6.2 ≤ z ≤ 14.8
You are looking at a (very degenerate) linear program.
Your unknown vector x = [v; f; z], the objective coeff vector is c = [-0.00175; 8.693; -0.00159].
In your particular case, you have no equality or inequality constraints, only lower and upper bounds lb = [124.53; 0.025; 6.2] and ub = [167.03; 0.083; 14.8] respectively.
You are trying to minimize
argmin c^T x
s.t. lb <= x <= ub
Use linprog
x = linprog( c, [], [], [], [], lb, ub );
Note that the constant coefficient 0.237 is not participating in the optimization since it has no effect on the argmin.
You could use fmincon. Let's say v = x(1), f = x(2), and z = x(3) so that tehy are all in a vector. First you define a handle to your function as:
h = #(x)(0.237 - − 0.00175*x(1) + 8.693*x(2) − 0.00159*x(3))
You need to define your constraints in the form A*x ≤ b. In your example, we rewrite the constraints as both being less than some function:
v ≤ 167.03
-v ≤ -124.53
f ≤ 0.083
-f ≤ -0.025
z ≤ 14.8
-z ≤ -6.2
Now you can convert the above into matrix form A*x ≤ b:
A = [1, 0, 0;
-1, 0, 0;
0, 1, 0;
0, -1, 0;
0, 0, 1;
0, 0, -1]
b = [167.03; -124.53; 0.083; -0.025; 14.8; -6.2]
Finally, you define an initial guess x0 for your variable x, and optimize by calling fmincon as:
xOpt = fmincon(h, x0, A, b)
The initial guess can be any values that lie within your constraints, so x0 = [167; 0.08; 14] could work.
EDIT: You could also define your lower and upper bound vectors as lb = [124.53; 0.025; 6.2] and ub = [167.03; 0.083; 14.8] and call fmincon using:
xOpt = fmincon(h, x0, [], [], [], [], lb, ub)
Note: if you want to randomize your initial guess, you can use your lb and ub vectors to do so with x0 = lb + (ub - lb)*rand().
This is a bit more powerful of a tool than you need for this problem, but hey, teach a man to fish.
Well, it's very simple:
>> Min_Ra = 0.237 − 0.00175*167.03 + 8.693*0.025 − 0.00159*14.8
ans =
0.1384905
:)
(just subtract the maximum permissible amounts while adding the minimum permissible amounts)