Formatting equations for linprog in MATLAB - matlab

I have to solve a simple problem using function linprog in matlab math toolbox. The problem is that I don't know how to format my equations so this function solves the problem.
This is the function I am trying to minimize (a_i are some given coefficients, x is in R^5):
x = argmax min{a1*x1 + a2*x2, a2*x2 + a3*x3 + a4*x4, a4*x4 + a5*x5}
subject to:
sum(x_i) = 3000
all x_i >= 0
This could be rephrased as:
(x, lambda) = argmin(-lambda)
subject to:
a1*x1 + a2*x2 >= lambda
a2*x2 + a3*x3 + a4*x4 >= lambda
a4*x4 + a5*x5 >= lambda
sum(x_i) = 3000
all x_i >= 0
I could only find examples of minimization of simple linear functions without min/max arguments in it. Could you give me a hint how to make my structures as arguments for linprog function?

Let's try the following
your x vector is now
[x1 x2 x3 x4 x5 lambda]
the objective vector
f = [0 0 0 0 0 -1]
equality constraint:
Aeq = [1 1 1 1 1 0] beq = 3000
Inequality constraint:
A = [-a1 -a2 0 0 0 1; 0 -a2 -a3 -a4 0 1; 0 0 0 -a4 -a5 1] b = [0;0;0]
lower bound:
lb = [0 0 0 0 0 -inf]
now try
linprog( f, A, b, Aeq, beq, lb )
up to some transposing of arguments should do the trick.

I don't believe you can pose the question as you phrased it as a linprog problem. The "MIN" operation is the problem. Since the objective function can't be phrased as
y = f'x.
Even though your constraints are linear, your objective function isn't.
Maybe with some trickery you can linearize it. But if so, that's a math problem. See: https://math.stackexchange.com/

Related

MATLAB Curve Fitting with Slopes

Background
I am currently working on a lecture for my Engineering in MATLAB course and stumbled upon a problem I would like to present to the class. I have made many different attempts to solve this problem, but my graphs keep coming out incorrect. I will describe the problem below and all the steps I took to try to solve this problem.
Problem
Find the coefficients of the fourth-degree polynomial
P(x) = ax^4 + bx^3 + cx^2 + dx + e
whose graph goes through the points (0, 1), (1, 1), (-1,3), and
whose slopes at x = -1 is 20 and at x = 1 is 9.
Check your answer visually.
Attempt
I began by creating a matrix of the above x-values that I have derived as follows:
A = [0^4 0^3 0^2 0 1; 1^4 1^3 1^2 1 1; (-1)^4 (-1)^3 (-1)^2 -1 1];
A = [0 0 0 0 1; 1 1 1 1 1; 1 -1 1 -1 1];
This creates a 5 column by 3 row matrix that I may use to plot the polynomial.
My issue is that I am unable to get the last row of x-values, since each row is an equation in the system of equations and there must be as many equations as there are unknowns (4: a, b, c, and d are unknown, but e always equals 1 as you can see).
Ignoring this issue for a moment, I can continue to create a vertical matrix of y-values so that I may solve the system of equations. These y values are already given, so all I have to do is type this code in:
y = [1 1 3]';
Once again, there should be a fourth y-value to go along with the system of equations, but I have been unable to derive it using just the slopes of the points at x = -1 and x = 1.
Once both the x-values and the y-values are derived, we can proceed to using the backslash operator (/) to solve the system of linear equations A*x = y.
p = A\y;
mldivide is more info on the mldivide function for anyone who needs reference.
From here on out, the following code which creates a polynomial from this system of equations and graphs it, should stay the same.
u = -1:.01:1;
v = polyval(p, u);
plot(u,v);
In this code, u is the domain of x-values from -1 to 1 with a 0.01 interval. This is needed by us to use the polyval function, which creates a polynomial from a system of equations we derived at p on the interval u.
Lastly, plot simply graphs our derived polynomial using MATLAB's GUI on the interval u.
As you can see, the only missing pieces I have are one more row of x-values in my matrix A and one y-value in matrix y that I need to find the four unknowns a, b, c, and d. I believe you must use the two slopes given in the problem to find each point. I have tried using the polyder function to get the derivative of the matrix p by doing,
q = polyder(p);
but I am still confused as to how to continue from there. Any help will be greatly appreciated.
I would calculate the derivative of the polynomial:
dP(x) = 4ax^3 + 3bx^2 + 2cx + d
Now, you know that dP(-1)=20 and dP(1)=9 so you have 5 equations with 5 unknowns:
e = 1
a + b + c + d + e = 1
a - b + c - d + e = 3
-4*a + 3*b - 2*c + d = 20
4*a + 3*b + 2*c + d = 9
So you can construct a 5x5 matrix and solve the system, as you did with A\y.
The code to construct this 5x5 matrix is:
A = [0 0 0 0 1 ; 1 1 1 1 1 ; 1 -1 1 -1 1 ; -4 3 -2 1 0 ; 4 3 2 1 0];
y = [1 1 3 20 9]';
You can then check the results on a plot:
p=A\y;
u = -1:.01:1;
v = polyval(p, u);
data_pts = [0, 1; 1, 1;-1, 3]
plot(u,v,data_pts(:,1),data_pts(:,2),'rx')
which gives the following plot:
You can do the same with the derivative and checks it goes through the points (-1,20) and (1,9).

matlab objective funtion must return a scalar value

I am trying to understand now fminunc (fmincon) works, however I keep getting error.
When I use documentation example with two variables
fun = #(x)3*x(1)^2 + 2*x(1)*x(2) + x(2)^2 - 4*x(1) + 5*x(2);
x0 = [1,1];
[x,fval] = fminunc(fun,x0);
everything works fine.
Hovewer, when I am trying to fit a plane for 3 points,
the code does not work
n0 = [ 0 1 -2;
1 2 1;
-2 -4 -4]
fun = #(x) [x(1) x(2) x(3)] * n0 - [1 1 1]
The task for fminunc is just an example. I know I can solve it easily analytically.
The cost function returns a scalar. What you have written returns a [1x3] matrix. You could try something like this if you want to minimise the euclidean distance
fun = #(x) sum(([x(1) x(2) x(3)] * n0 - [1 1 1]).^2);

Programming Finite Element Method

I am trying to teach myself how about Finite Element Methods.
All of my code is adapted from the following link pages 16-20
http://homepages.cae.wisc.edu/~suresh/ME964Website/M964Notes/Notes/introfem.pdf
I am programming along in Matlab to perform a finite element analysis on a single 8 node cube element. I have defined the xi,eta,zeta local axes (we can think about this as x, y, z for now), so I get the following shape functions:
%%shape functions
zeta = 0:.01:1;
eta = 0:.01:1;
xi = 0:.01:1;
N1 = 1/8*(1-xi).*(1-eta).*(1-zeta);
N2 = 1/8*(1+xi).*(1-eta).*(1-zeta);
N3 = 1/8*(1+xi).*(1+eta).*(1-zeta);
N4 = 1/8*(1-xi).*(1+eta).*(1-zeta);
N5 = 1/8*(1-xi).*(1-eta).*(1+zeta);
N6 = 1/8*(1+xi).*(1-eta).*(1+zeta);
N7 = 1/8*(1+xi).*(1+eta).*(1+zeta);
N8 = 1/8*(1-xi).*(1+eta).*(1+zeta);
The [N] Matrix is to be arranged like this according to the text I am reading:
%N Matrix
N= [N1 0 0 N2 0 0 N3 0 0 N4 0 0 N5 0 0 N6 0 0 N7 0 0 N8 0 0;
0 N1 0 0 N2 0 0 N3 0 0 N4 0 0 N5 0 0 N6 0 0 N7 0 0 N8 0;
0 0 N1 0 0 N2 0 0 N3 0 0 N4 0 0 N5 0 0 N6 0 0 N7 0 0 N8];
To find the [B] matrix i have to use the following [D] matrix:
%%Del Matrix for node i
%[ d/dx 0 0
% 0 d/dy 0
% 0 0 d/dz . . .
% d/dy d/dx 0
% 0 d/dz d/dy
% d/dz 0 d/dx ]
which is an operator to go on [N]. (B=DN)
Later on, as the text shows, I will be making calculations involving integrals of this [B] matrix over the volume of this element.
So, my question is, how can I store these polynomial shape functions in a matrix, operate on them with differentiation, and then integrate them numerically. I can tell with the way I have this set up right now, that it wont work because I have defined the functions as a vector over an interval [0,1] and then storing these vectors in the [N] matrix. Then using diff() function to differentiate appropriately to find the [B] matrix.
But since the matrix elements of [B] are now vectors over an interval [0,1] I think that is going to cause problems. How would you guys go about these calculations described in the textbook I posted above?
Solved my problem using anonymous functions and storing the polynomials in a symbolic matrix. example:
syms xi eta zeta
N1= ... %type out in terms of xi eta and zeta
.
.
.
dN1dXi = diff(N1,xi) %symbolic differentiation with respect to xi
can also perform symbolic integration when needed:
intN1 = int(N1,xi,lowerLimit,upperLimit) %symbolic integration with respect to xi
and when ready to substitute in actual values to evaluate the symbolic functions:
subs(N1,{xi,eta,zeta},{value1,value2,value3})
You should check page 24 about how to map from a parametric domain ([0,1]^) to the physical domain.
Although I think you can do as you said, using symbolic. I think symbolic calculation in Matlab is very time-consuming.
I would go for derivate N manually and store as dN, and use it when need it.
Regards,
German
after you have the shape functions you need to substitute it in the stiffness matrix, the stiffness matrix should be 24x24 as you have 24 degrees of freedom. to solve you need to build a linear system (Ax=b), the right hand side is based on the PDE you are solving and you have to include neuman boundary conditions in the right hand side plus the source term. In python for 2d element (4 DOF) will be like:
def shapefxncoef (Valxy):
#creating a temporary metrix to store zeros and get the size of the shape
#function matrix.
n_temp = np.zeros((4,4))
#filling the values of the matrix with a loop.
for i in range(4):
#the values used in the matrix are from the Valxy x and y components.
xi = Valxy [0, i];
yi = Valxy [1, i];
n_temp[i, 0] = 1;
n_temp[i, 1] = xi;
n_temp[i, 2] = yi;
n_temp[i, 3] = xi*yi;
#this gives an identity matrix and the stiffness matric can be derived
#if we take the inverse.
n = np.linalg.inv(n_temp);
return n;
def N (Valxy, x, y):
n = shapefxncoef (Valxy);
res = n[0, :] + n[1, :]*x + n[2, :]*y + n[3, :]*x*y;
return res;
def Be (Valxy, x, y):
res = np.zeros ((2,4));
res_temp = shapefxncoef (Valxy);
for i in range (4):
res_tempi = res_temp[:, i];
dNix = res_tempi[1] + res_tempi[3]*y;
dNiy = res_tempi[2] + res_tempi[3]*x;
res[0, i] = dNix;
res[1, i] = dNiy;
return res;
def Ke (Valxy, conduct):
a = lambda x, y: conduct * np.dot ((Be(Valxy, x, y)).T, Be(Valxy, x, y));
k = intr.integrateOnQuadrangle (Valxy.T, a, np.zeros((4,4)));
return k;

equationsToMatrix: how do I get the values of the variables?

When using equationsToMatrix you solve a set of linear equations as in the example (the solution is included)
syms x y z;
[A, b] = equationsToMatrix([x + y - 2*z == 0, x + y + z == 1, 2*y - z + 5 == 0], [x, y, z])
%solution of the equation set
A =
[ 1, 1, -2]
[ 1, 1, 1]
[ 0, 2, -1]
b =
0
1
-5
The vector b returns the values of the variables at issue: x,y, and z. However if I type x then MATLAB returns x and not 0, which is the solution of the equation in this case. This also occurs without adding the syms option.
The other problem is that if I type b(1) or b(2), I don't get any value: I would expect b to contain the values of x,y and z.
What I would need is to get something like this in the end
b(1) = 0
or
x = 0
What should I do to get the values of x,y,z by just typing x,y,z?
What you have is a way of converting symbolic linear equations into a numeric system by extracting the coefficient matrices. To solve the system you need to do
sol = A\b;
and now you can use the values in another expression with
subst(expr, {x,y,z}, {sol(1),sol(2),sol(3));
for example
A =
1 1 -2
1 1 1
0 2 -1
b =
0
1
-5
>> A\b
ans =
3.0000
-2.3333
0.3333

matlab: lsqcurvefit and parameters

I'm trying to do some fitting with lsqcurvefit. I have a function like that:
function F = cdf_3p_model(a,data)
F=1-((1-a(5)-a(6)).*(exp(-abs(data)./a(1)))+((1-a(4)-a(6)).*(exp(-abs(data)./a(2))))+((1-a(4)-a(5)).*(exp(-abs(data)./a(3)))));
and
function [a residual] = cdf_fit_3p(x,y)
a0 = [10 1 0.1 0.3 0.3 0.3];
lb = [0 0 0 0 0 0];
ub = [];
curvefitoptions = optimset('Display','final','MaxFunEvals',100000,'MaxIter',50000);
[a, residual] = fmincon(#cdf_3p_model,a0,x,y,lb,ub,curvefitoptions);
end
I set the initial parameters, ub, lb but how do I also declare that:
a(1) > a(2) > a(3)
a(5) + a(6) +a(7) = 1
I think you have better chance using one of the minimization routines such as fmincon which allows you to specify constraints you might otherwise be unable to do. You can easily incorporate least-squares by taking the L2-norm of the difference between model and data
Normally I would say, "make clauses in your function that gives really terrible 'scores' when those conditions are not met." However, your conditions make the range of allowable parameters such a tiny, tiny subset of the range of possible numbers that I think you would cause lsqcurvefit to never converge if you do that. I would say lsqcurvefit is not the right solution for you.
You will have to calculate the parameters you "want" from a set of parameters that's more usable to MatLab.
For example, you can rewrite
a(1) > a(2) > a(3)
a(5) + a(6) + a(7) = 1
as
a(3) = p(1)
a(2) = p(1) + p(2)
a(1) = p(1) + p(2) + p(3)
a(4) = p(4)
a(5) = p(5)
a(6) = p(6)
a(7) = 1 - p(5) - p(6)
with
lb = [0 0 0 0 0 0]
ub = [Inf Inf Inf Inf 1 1]
Well, it's not perfect, because it allows a(7) as low as -1 instead of 0. But it includes your other constraints.