Error using Algebraic Constraint in Simulink - matlab

I want to implement an equation
c= a*w*(sinwt + b*sin(2*w*t))
where w is varying and a,b and c are all constants.
I have done it using Agebraic Constraint block but I am getting an error
Trouble solving algebraic loop containing 'trial1/Algebraic Constraint1/Initial Guess' at time >0. Stopping simulation. There may be a singularity in the solution. If the model is correct, >try reducing the step size (either by reducing the fixed step size or by tightening the error >tolerances)
Pl help as in what might be wrong. Or suggest what are the other ways of solving the equation and finding a graph of w vs t(using scope).

Try implementing equation in this manner.
I have taken a=1,b=1,c=1 & w=1
c= #(t) (a*w*(sin(t) + b*sin(2*w*t)));
t = linspace (-pi,pi,1000);
figure
plot (t,c(t))

Related

Efficient solver for solving given MINLP problem. (Preferably open source)

I am trying to solve an IP optimization problem using DoCPLEX. Here (x_p, y_p) are co-ordinates of points, R is a positive constant and M is a very large positive number.
DECISION VARIABLES:
x_c[j], y_c[j]: Continuous variables
g[i,j]: Binary variables
z[j]: Binary variables
OBJECTIVE FUNCTION:
sum(z[j]) for all j
One set of CONSTRAINTS:
R - ((x_p[i]-x_c[j])**2 + (y_p[i] - y_c[j])**2)**0.5 <= M*g[i,j] for all i and j
I tried the following:
For g[i,j]=0, x_p=0 and y_p=0, we have following equation:
x_c^2 + y_c^2 >= R^2, which is non convex
Tried solving the above problem using docplex.mp.model and got the following error:
Error: Model has non-convex quadratic constraint, name='C1_00'
Tried solving the above problem using docplex.cp.model, assuming all decision variables can take only integer values
Problem is solvable, but even smaller sized problem involving ~210 binary variable was solved in 45 hours.
Suggest a solver that can solve above MINLP problems (Objective value is discrete and one set of constraints is non-linear) relatively faster. As I need to solver larger problem instances that may involve 10,000+ binary variables within reasonable computation time. Any suggestion will be helpful.

Optimization with an inherent differential equation

I am trying to solve an optimization problem where the given objective function is to minimize
norm(R - R*)
where R is obtained by solving a boundary value problem (BVP) and `R* is a known value.
For example:
R = (p1 + p2*p3);
The BVP is given by
p1dot = p2 + p3 + x1;
p2dot = x2 + p1*p2;
p3dot = x3 + p1*p2*p3;
where x1, x2 and x3 are the optimized variables.
I am trying to solve this in MATLAB using fmincon where the BVP is solved in the objective function at every guess by the solver to estimate norm(R-R*).
EDIT 1 : I have a non linear inequality constraint which is a function of pi where i = 1,2,3. This is the reason for choosing fmincon
The issue I am facing is that during some guesses the BVP doesn't converge and the optimization stops. I guess the issue is some guess value to solve the BVP doesn't satisfy. If I give bounds to the variables then it solves the optimization problem. I am not really aware of the bounds on variables beforehand.
In order to overcome this I want to detect the guess when BVP is not solved, save them and force the optimization routine to not try values when it cannot be solved by going back to its previous guess. How do we implement this in fmincon routine?
EDIT 2 : I figured out the issue is with initial guess provided to the BVP solver. For smaller values of the optimization variables x this guess is good but when the magnitude of x increase this guess is not good enough.
Is there a way to update the solution of 'BVP' in the previous iteration as the initial guess for the next iteration in optimization. Intuitively this should fix it. Please let me know if there are some loop holes in this method.

matlab differential equation

I have the following differential equation which I'm not able to solve.
We know the following about the equation:
D(r) is a third grade polynom
D'(1)=D'(2)=0
D(2)=2D(1)
u(1)=450
u'(2)=-K * (u(2)-Te)
Where K and Te are constants.
I want to approximate the problem using a matrix and I managed to solve
the similiar equation: with the same limit conditions for u(1) and u'(2).
On this equation I approximated u' and u'' with central differences and used a finite difference method between r=1 to r=2. I then placed the results in a matrix A in matlab and the limit conditions in the vector Y in matlab and ran u=A\Y to get how the u value changes. Heres my matlab code for the equation I managed to solve:
clear
a=1;
b=2;
N=100;
h = (b-a)/N;
K=3.20;
Ti=450;
Te=20;
A = zeros(N+2);
A(1,1)=1;
A(end,end)=1/(2*h*K);
A(end,end-1)=1;
A(end,end-2)=-1/(2*h*K);
r=a+h:h:b;
%y(i)
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
A(2:end-1,2:end-1)=A(2:end-1,2:end-1)+diag(yi);
%y(i-1)
for i=1:1:length(r)-1
ymin(i)=r(i+1)*(1/(h^2))-1/(2*h);
end
A(3:end-1,2:end-2) = A(3:end-1,2:end-2)+diag(ymin);
%y(i+1)
for i=1:1:length(r)
ymax(i)=r(i)*(1/(h^2))+1/(2*h);
end
A(2:end-1,3:end)=A(2:end-1,3:end)+diag(ymax);
Y=zeros(N+2,1);
Y(1) =Ti;
Y(2)=-(Ti*(r(1)/(h^2)-(1/(2*h))));
Y(end) = Te;
r=[1,r];
u=A\Y;
plot(r,u(1:end-1));
My question is, how do I solve the first differential equation?
As TroyHaskin pointed out in comments, one can determine D up to a constant factor, and that constant factor cancels out in D'/D anyway. Put another way: we can assume that D(1)=1 (a convenient number), since D can be multiplied by any constant. Now it's easy to find the coefficients (done with Wolfram Alpha), and the polynomial turns out to be
D(r) = -2r^3+9r^2-12r+6
with derivative D'(r) = -6r^2+18r-12. (There is also a smarter way to find the polynomial by starting with D', which is quadratic with known roots.)
I would probably use this information right away, computing the coefficient k of the first derivative:
r = a+h:h:b;
k = 1+r.*(-6*r.^2+18*r-12)./(-2*r.^3+9*r.^2-12*r+6);
It seems that k is always positive on the interval [1,2], so if you want to minimize the changes to existing code, just replace r(i) by r(i)/k(i) in it.
By the way, instead of loops like
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
one usually does simply
yi=-r*(2/(h^2));
This vectorization makes the code more compact and can benefit the performance too (not so much in your example, where solving the linear system is the bottleneck). Another benefit is that yi is properly initialized, while with your loop construction, if yi happened to already exist and have length greater than length(r), the resulting array would have extraneous entries. (This is a potential source of hard-to-track bugs.)

Vectorised Function in numjac MatLab

I am trying to solve a large DAE system, coupled with the equations to calculate the sensitivity of the variables to a parameter. My problem is the jacobian of the entire system, its calculation is pretty slow and I would like to speed it up.
I am using numjac, in this form:
[Jx,FAC,G] = numjac(#(t,y)MODEL(t,y,X),tt,yy,dydt,jac_tol,FAC,0,JPat,G);
I want to vectorize the code, but I can't seem to get what this means. As far as I understood my code is already vectorized! t,y,X go in and I get a column vector of dy(i)/dt, or F(t,y(i)). But if I say that my function is vectorized, I get an error:
Matrix dimensions must agree.
Error in numjac (line 192)
Fdiff = Fdel - Fty(:,ones(1,ng));
How can I properly vectorize it?

How to solve complex system of equations using matlab?

I have to analyze 802.11 saturation throughput using matlab, and here is my problem. I'm trying to solve parametric equations below (parameters are m,W,a) using solve function and i get
Warning: Explicit solution could not be found
How could I solve above equations using matlab?
I guess you were trying to find an analytical solution for tau and p using symbolic math. Unless you're really lucky with your parameters (e.g. m=1), there won't be an analytical solution.
If you're interested in numerical values for tau and p, I suggest you manually substitue p in the first equation, and then solve an equation of the form tau-bigFraction=0 using, e.g. fzero.
Here's how you'd use fzero to solve a simple equation kx=exp(-x), with k being a parameter.
k = 5; %# set a value for k
x = fzero(#(x)k*x-exp(-x),0); %# initial guess: x=0