Scipy: solve quadratic with double summation - scipy

I'm relatively new to scipy and I'm looking for a guidance.
I have an unconstrained minimization problem of the form:
Where a and b are coefficients and x is a vector of unknowns (can be of different length). I'm wondering about how to solve this problem using python. I looked through the scipy reference guide but couldn't find the answer.
Thank you

Let A be the sum of the a's and X that of the x's. Then you want to choose X to minimise
Sum{ i | square( A-X-b[i])}
The value of X that minimises this will be the mean (over i) of A-b[i], that is A - mean(b)

Related

Is there a less time-consuming way to solve a Symmetric Matrix Equation

I'm currently working on solving an equation that involves a symmetric matrix C with 4 unknown variables, and a vector A of the same dimension. The equation I'm trying to solve is:
[C]*{A}=0 (1)
where * denotes matrix multiplication and { } denotes a vector.
The dimension of the C matrix can be as large as 100x100 or more, and I'm trying to define the unknown variables in C in a way that solves equation (1). One approach I've tried is to calculate the determinant of C, |[C]|=0, and solve for the 4 different variables inside.
However, when the dimension of the matrix is large, my current method in Mathematica is not able to solve the problem. I'm wondering if anyone has any suggestions for me to solve this equation more efficiently.
I'm open to using other programming languages such as Matlab or Python as well. Any suggestions or advice would be greatly appreciated.
Thank you in advance for your help and guidance.
I have tried solving this problem with 25*25 dimension. However, this size is not enough for the percision that I want for the variables.

solving a galois field matrix equation in matlab

I have the equation AX * C = AXB where all of the variables are square gf matrices of size n and the values are 0 or 1. AXB and AX are known, while C should be solved to B or an equivalent of it (there should be multiple solutions). Because X most likely isn´t regular (rank < n) not all of the variables in C can be calculated and i have to guess the rest after solving.
Question is: Can Matlab solve this equation (as far as possible), and in a better way than "manually" splitting the equation into n^2 equations?
I already tried to tell matlab that C is a symbol of, let´s say [8,8] and to solve AX*C==AXB, but solve doesn´t seem to work with galois fields.
Any hints on how to do this would be appreciated.

Matlab: Reduce second order matrix differential equation to standard eigenproblem

I want to obtain the natural frequencies of a simple mechanical system with mass matrix M and stiffness matrix K (.mat-file -> Download):
Mx''(t)+Kx(t)=0 (x= Position).
It means basically, that I have to solve det(K-w^2*M)=0. But how can I solve it in Matlab (or if necessary reduce it to a standard eigenvalue problem and solve it then)? The matrices are definitely solvable with Abaqus (FEM Software), but I have to solve it in Matlab.
I tried the following without success: det(K-w^2*M)=0 => det(M^-1*K-w^2*I)=0 (I := unity matrix)
But solving this eigenvalue problem with
sqrt(eigs(K*M^-1))
delivers wrong values and the warning:
"Matrix is singular to working precision.
In matlab.internal.math.mpower.viaMtimes (line 35)"
Other wrong values can be obtained via det(K-w^2*M)=0 => det(I/(w^2)-M*K^-1)=0:
1./sqrt(eigs(M*K^-1))
Any hint would help me. Thanks in advance.
As #Arpi mentioned, you actually want to solve the generalized eigenvalue problem:
K*x = w^2*M*x
Since your matrices K and M are apparently singular (or just one of them), it is not possible to use eigs, but you have to use eig:
V = eig(K,M);
w = sqrt(V);

Matlab left - division in vectors?

x=[1;2;3]
x =
1
2
3
y=[4;5;6]
y =
4
5
6
x\y
ans =
2.2857
How did Matlab find that result ? (I searched many forums but I did not understand what they told.I would like to know the algorithm which gave this result.)
From MATLAB documentation of \:
If A is an M-by-N matrix with M < or > N and B is a column vector with M components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations A*X = B.
Here your system is not under/over-determined. Since both have 3 rows. So you can visualize your equation as:
xM=y
M=inv(x)*y
Now, since your matrix is not square, it will calculate the pseudo-inverse using SVD. Therefore,
M=pinv(x)*y;
You will get value of M as 2.2857.
Another explanation can be: It will give you the solution of xM=y in the sense of least squares. You can verify this as follows:
M=lsqr(x,y)
This will give you the value of M = 2.2857.
You can always do help \ in MATLAB command window to get more information.
You are encouraged to check more details about the least squares and pseudo-inverse.
This documentation should explain it
http://www.mathworks.com/help/matlab/ref/mrdivide.html
Here is a link to the algorithm
http://www.maths.lth.se/na/courses/NUM115/NUM115-11/backslash.html
You can see the source inside matlab much more easily though. (I don't have it locally so I can't check but the source of a lot of matlab functions is available inside matlab)

How can I solve a Volterra (Fredholm?) integral equation in Matlab

I'm talking about Volterra integral equations of second order:
In my case g is an ugly integral also between a and x, also a=0 (for both g and the integral above). K is equal to 1.
I found some information about Fredholm equations, but they are not exactly the same (fixed intervals, they don't have x on the integral sign), I wonder if maybe I can reconduct my analysis to a Fredholm equation? And if so, how can I solve it in Matlab?
You have a Volterra equation of the second kind, and in the case when the kernel takes the form k(x-t) it can be solved an operational way(method).
Since your task is not unique, will be convenient to use a ready solution:
http://www.mathworks.com/matlabcentral/fileexchange/49721-volterra-integral-equations-solver
Solve your equation (if I understood correctly problem statement):
isolve(1,10*x,1)
ans =
10*exp(x) - 10
Will check the correctness of the solution by substituting in the source:
10*x + int(y,0,x)
ans =
10*exp(x) - 10
Solved correctly.
P.S. Sorry for my English.