Solving optimal control problems, ode45 vs fmincon - matlab

Good afternoon!
First things first, I looked for similar questions for a while, but (probably because of my inexperience) I've found nothing similar to what I'm going to ask.
I'm using matlab for the first time to solve this kind of problems, so I'm not sure of what to do. A brief explenation:
I'm doing a project for my Optimal Control course: I have to replicate the results of a paper about employment, and I'm stuck with the plots. I have the following data:
five variable functions (U(t), T(t), R(t), V1(t) and V2(t))
four control functions(u1(t), u2(t), u3(t), u4(t))
constraints on the control variables (each u must be between 0 and 1)
initial values for U, T, R, V1 and V2 (in t=0, in particular V1 and V2 are constant over time)
final values for the λ coefficients in the hamiltonian
(note: for the controls, I've already found the optimal expression, which is in this form: ui = min{1, max{0,"expression"}}. If needed, I can give also the four expressions, neglected to
synthesize a little)
Under professor's suggestions, I've tried to use fmincon, that theoretically should give me directly the information that I need to plot some result using only the cost function of the problem. But in this case I have some issues involving time in the calculations. Below, the code that I used for fmincon:
syms u
%note: u(5) corresponds to U(t), but this is the only way I've found to get
%a result, the other u(i) are in ascending order (u(1) = u1 and so on...)
g = #(u) 30*u(5) + (20/2)*(u(1))^2 + (20/2)*(u(2))^2 + (10/2)*(u(3))^2 + (40/2)*(u(4))^2;
%initial guesses
u0 = [0 0 0 0 100000]; %
A = [];
b = [];
Aeq = [];
beq = [];
lb = 0.0 * ones(1,2,3,4);
ub = 1.0 * ones(1,2,3,4);
[x,fval,output,lambda] = fmincon(g, u0, A, b, Aeq, beq, lb, ub);
Whit this code, i get (obviously) only one value for each variable as result, and since I've not found any method to involve time, as I said before, I start looking for other solving strategies.
I found that ode45 is a differential equation solver that has the "time iteration" already included in the algorithm, so I tried to write the code to get it work with my problem.
I took all the equations from the paper and put them in a vector as shown in the mathworks examples, and this is my matlab file:
syms u1(t) u2(t) u3(t) u4(t)
syms U(t) T(t) R(t) V1(t) V2(t)
syms lambda_u lambda_t lambda_r lambda_v1 lambda_v2
%all the parameters provided by the paper
delta = 500;
alpha1 = 0.004;
alpha2 = 0.005;
alpha3 = 0.006;
gamma1 = 0.001;
gamma2 = 0.002;
phi1 = 0.22;
phi2 = 0.20;
delta1 = 0.09;
delta2 = 0.05;
k1 = 0.000003;
k2 = 0.000002;
k3 = 0.0000045;
%these two variable are set constant
V1 = 200;
V2 = 100;
%weight values for the cost function (only A1 is used in this case, but I left them all since the unused ones are irrelevant)
A1 = 30;
A2 = 20;
A3 = 20;
A4 = 10;
A5 = 40;
%ordering the unknowns in an array
x = [U T R u1 u2 u3 u4];
%initial conditions, ordered as the x vector (for the ui are guesses)
y0 = [100000 2000 1000 0 0 0 0];
%system set up
f = #(t,x) [delta - (1 + x(4))*k1*x(1)*V1 - (1 + x(5))*k2*x(1)*V2 - alpha1*x(1) + gamma1*x(2) + gamma2*x(3);...
(1 + x(4))*k1*x(1)*V1 - k3*x(2)*V2 - alpha2*x(2) - gamma1*x(2);...
(1 + x(5))*k2*x(1)*V2 - alpha3*x(3) - gamma2*x(3) + k3*x(2)*V2;...
alpha2*x(2) + gamma1*x(2) + (1 + x(6))*phi1*x(1) + k3*x(2)*V2 - delta1*V1;...
alpha3*x(3) + gamma2*x(3) + (1 + x(7))*phi2*x(1) - delta2*V2;...
-A1 + (1 + x(4))*k1*V1*(lambda_u - lambda_t) + (1 + x(5))*k2*V2*(lambda_u - lambda_r) + lambda_u*alpha1 - lambda_v1*(1 + x(6))*phi1 - lambda_v2*(1 + x(7))*phi2;...
-lambda_u*gamma1 + (alpha2 + gamma1)*(lambda_t - lambda_v1) + k3*V2*(lambda_t - lambda_r - lambda_v1);...
-lambda_u*gamma2 + (alpha3 + gamma2)*(lambda_r - lambda_v2);...
(1 + x(4))*k1*x(1)*(lambda_u - lambda_t) + lambda_v1*delta1;...
(1 + x(5))*k2*x(1)*(lambda_u -lambda_r) + k3*x(2)*(lambda_t - lambda_r - lambda_v1) + lambda_v2*delta2];
%using ode45 to solve over the chosen time interval
[t,xa] = ode45(f,[0 10],y0);
With this code, I get the following error:
Error using odearguments (line 95)
#(T,X)[DELTA-(1+X(4))*K1*X(1)*V1-(1+X(5))*K2*X(1)*V2-ALPHA1*X(1)+GAMMA1*X(2)+GAMMA2*X(3);(1+X(4))*K1*X(1)*V1-K3*X(2)*V2-ALPHA2*X(2)-GAMMA1*X(2);(1+X(5))*K2*X(1)*V2-ALPHA3*X(3)-GAMMA2*X(3)+K3*X(2)*V2;ALPHA2*X(2)+GAMMA1*X(2)+(1+X(6))*PHI1*X(1)+K3*X(2)*V2-DELTA1*V1;ALPHA3*X(3)+GAMMA2*X(3)+(1+X(7))*PHI2*X(1)-DELTA2*V2;-A1+(1+X(4))*K1*V1*(LAMBDA_U-LAMBDA_T)+(1+X(5))*K2*V2*(LAMBDA_U-LAMBDA_R)+LAMBDA_U*ALPHA1-LAMBDA_V1*(1+X(6))*PHI1-LAMBDA_V2*(1+X(7))*PHI2;-LAMBDA_U*GAMMA1+(ALPHA2+GAMMA1)*(LAMBDA_T-LAMBDA_V1)+K3*V2*(LAMBDA_T-LAMBDA_R-LAMBDA_V1);-LAMBDA_U*GAMMA2+(ALPHA3+GAMMA2)*(LAMBDA_R-LAMBDA_V2);(1+X(4))*K1*X(1)*(LAMBDA_U-LAMBDA_T)+LAMBDA_V1*DELTA1;(1+X(5))*K2*X(1)*(LAMBDA_U-LAMBDA_R)+K3*X(2)*(LAMBDA_T-LAMBDA_R-LAMBDA_V1)+LAMBDA_V2*DELTA2]
returns a vector of length 10, but the length of initial conditions vector is 7. The vector returned by
#(T,X)[DELTA-(1+X(4))*K1*X(1)*V1-(1+X(5))*K2*X(1)*V2-ALPHA1*X(1)+GAMMA1*X(2)+GAMMA2*X(3);(1+X(4))*K1*X(1)*V1-K3*X(2)*V2-ALPHA2*X(2)-GAMMA1*X(2);(1+X(5))*K2*X(1)*V2-ALPHA3*X(3)-GAMMA2*X(3)+K3*X(2)*V2;ALPHA2*X(2)+GAMMA1*X(2)+(1+X(6))*PHI1*X(1)+K3*X(2)*V2-DELTA1*V1;ALPHA3*X(3)+GAMMA2*X(3)+(1+X(7))*PHI2*X(1)-DELTA2*V2;-A1+(1+X(4))*K1*V1*(LAMBDA_U-LAMBDA_T)+(1+X(5))*K2*V2*(LAMBDA_U-LAMBDA_R)+LAMBDA_U*ALPHA1-LAMBDA_V1*(1+X(6))*PHI1-LAMBDA_V2*(1+X(7))*PHI2;-LAMBDA_U*GAMMA1+(ALPHA2+GAMMA1)*(LAMBDA_T-LAMBDA_V1)+K3*V2*(LAMBDA_T-LAMBDA_R-LAMBDA_V1);-LAMBDA_U*GAMMA2+(ALPHA3+GAMMA2)*(LAMBDA_R-LAMBDA_V2);(1+X(4))*K1*X(1)*(LAMBDA_U-LAMBDA_T)+LAMBDA_V1*DELTA1;(1+X(5))*K2*X(1)*(LAMBDA_U-LAMBDA_R)+K3*X(2)*(LAMBDA_T-LAMBDA_R-LAMBDA_V1)+LAMBDA_V2*DELTA2]
and the initial conditions vector must have the same number of elements.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in test (line 62)
[t,xa] = ode45(f,[0 10],y0);
For which I can't find a solution, since I have used all the initial values given in the paper. The only values that I have left are the final values for the lambda coefficients, since they are final values, and I am not sure if they can be used.
In this case, I can't also understand where I should put the bounds on the control variable.
For completeness, I will provide also the link to the paper in question:
https://www.ripublication.com/ijss17/ijssv12n3_13.pdf
Can you help me figure out what I can do to solve my problems?
P.S: I know this is a pretty bad code, but I'm basing on the basics tutorials on mathworks; for sure this should need to be refactored and ordered in various file (one for the cost function and one for the constraints for example) but firstly I would like to understand where the problem is and then I will put all in a pretty form.
Thank you so much!

Generally you confused something with Vectors. In initial conditions you declared 7 values:
%initial conditions, ordered as the x vector (for the ui are guesses)
y0 = [100000 2000 1000 0 0 0 0];
But you declared 10 ODE's:
%system set up
f = #(t,x) [delta - (1 + x(4))*k1*x(1)*V1 - (1 + x(5))*k2*x(1)*V2 - alpha1*x(1) + gamma1*x(2) + gamma2*x(3);...
(1 + x(4))*k1*x(1)*V1 - k3*x(2)*V2 - alpha2*x(2) - gamma1*x(2);...
(1 + x(5))*k2*x(1)*V2 - alpha3*x(3) - gamma2*x(3) + k3*x(2)*V2;...
alpha2*x(2) + gamma1*x(2) + (1 + x(6))*phi1*x(1) + k3*x(2)*V2 - delta1*V1;...
alpha3*x(3) + gamma2*x(3) + (1 + x(7))*phi2*x(1) - delta2*V2;...
-A1 + (1 + x(4))*k1*V1*(lambda_u - lambda_t) + (1 + x(5))*k2*V2*(lambda_u - lambda_r) + lambda_u*alpha1 - lambda_v1*(1 + x(6))*phi1 - lambda_v2*(1 + x(7))*phi2;...
-lambda_u*gamma1 + (alpha2 + gamma1)*(lambda_t - lambda_v1) + k3*V2*(lambda_t - lambda_r - lambda_v1);...
-lambda_u*gamma2 + (alpha3 + gamma2)*(lambda_r - lambda_v2);...
(1 + x(4))*k1*x(1)*(lambda_u - lambda_t) + lambda_v1*delta1;...
(1 + x(5))*k2*x(1)*(lambda_u -lambda_r) + k3*x(2)*(lambda_t - lambda_r - lambda_v1) + lambda_v2*delta2];
Every line in above code is recognized as one ODE.
But that's not all. The second problem is with your construction. You mixed symbolic math (lambda declared as syms) with numerical solving, which will be tricky. I'm not familiar with the exact scientific problem you are trying to solve, but if you can't avoid symbolic math, maybe you should try dsolve from Symbolic Math Toolbox?

Related

Gauss-Legendre Matlab: How to use Newton method to approximate k_1 and k_2?

Recently as a project I have been working on a program in matlab designed to implement the Gauss-Legendre order 4 method of solving an IVP. Numerical analysis- and coding in particular- is somewhat of a weakness of mine, and thus it has been rather tough going. I used the explicit Euler method to initially seed the method, but I've been stuck for a very long time on how to use the Newton method to get closer values of k1 and k2.
Thusfar, my code looks as follows:
`
%Gauss-Butcher Order 4
function[y] = GBOF(f,fprime,y_0,maxit,ertol)
A = [1/4,1/4-sqrt(3)/6;1/4+sqrt(3)/6,1/4];
h = [1,0,0,0,0,0,0,0,0,0,0,0,0];
t = [0,0,0,0,0,0,0,0,0,0,0,0,0];
for n = 1:12
h(n+1) = 1/(2^n);
t(n+1) = t(n)+h(n);
end
y = zeros(size(t));
y(1) = y_0;
niter = 1;
%Declaration of Variables
for i = 1:12
k = f(y(i));
y1approx = y(i) + ((1/2-sqrt(3))*h(i)*k);
y2approx = y(i) + ((1/2+sqrt(3))*h(i)*k);
k1 = f(y1approx);
k2 = f(y2approx);
%Initial guess for newton seeding
errorFunc =#(k1,k2) [k1-f(y(i) +A(1,1)*k1+A(1,2)*k2*h(i)); k2-f(y(i)+A(2,1)*k1+A(2,2)*k2*h(i))];
error = errorFunc(k1,k2);
%Function for error and creation of error variable
while norm(error) > ertol && niter < maxit
niter = niter + 1;
** k1 = k1-f(k1)/fprime(k1);
k2 = k2-f(k2)/fprime(k2);
** error = errorFunc(k1,k2);
%Newton Raphston for estimating K1 and K2
end
y(i+1) = y(i) +(h(i)*(k1+k2)/2);
%Creation of next
end
disp(t);
`
The part of the code I believe is causing this to fail is highlighted. When I enter in a basic ivp (i.e. y' = y, y(0) =1), I get the output
Any input on how I could go about fixing this would be much appreciated.
Thank you.
I have tried replacing the k1s and k2s in the problem with the values used in the formula extrapolated from the butcher tableau, but nothing changed. I can't think of any other ways to tackle this issue.
The implicit system you have to solve is
k1 = f(y + h*(a11*k1 + a12*k2) )
k2 = f(y + h*(a21*k1 + a22*k2) )
This is also correct in your residual function errorFunc.
The naive way is just to iterate this system, like any other fixed-point iteration.
This system has a linearization rel. h at the base point y
k1 = f(y) + h*f'(y)*(a11*k1 + a12*k2) + O(h^2)
k2 = f(y) + h*f'(y)*(a21*k1 + a22*k2) + O(h^2)
Seen as simple iteration, the contraction factor is O(h), so if h is small enough, the factor is smaller than 1 and thus convergence is sure, increasing the order of h in the residual by 1 in each step. So with 6 iterations the error in the implicit system is O(h^6), which is one order smaller than the local truncation error.
One can reduce the number of iterations if k1,k2 start with some higher-order estimates, not just with k1=k2=f(y).
One can reduce the right side residual by removing the terms that are linear in h (on both sides of course).
k1 - h*f'(y)*(a11*k1 + a12*k2) = f(y + h*(a11*k1 + a12*k2) ) - h*f'(y)*(a11*k1 + a12*k2)
k2 - h*f'(y)*(a21*k1 + a22*k2) = f(y + h*(a21*k1 + a22*k2) ) - h*f'(y)*(a21*k1 + a22*k2)
The right side is evaluated at the current values, the left side is a linear system for the new values. So
K = K - solve(M, rhs)
with
K = [ k1; k2]
M = [ 1 - h*f'(y)*a11, -h*f'(y)*a12 ; -h*f'(y)*a21, 1 - h*f'(y)*a22 ]
= I - h*f'(y)*A
rhs = [ k1 - f(y + h*(a11*k1 + a12*k2) ); k2 - f(y + h*(a12*k1 + a12*k2) )
= K - f(Y)
where
Y = y+h*A*K
This should, probably, work for scalar equations, for systems this involves Kronecker products of matrices and vectors.
As the linear part is taken out of the residual, the contraction factor in this new fixed-point iteration is O(h^2), possibly with smaller constants, so it converges faster and, it has been argued, for larger step sizes.
What you have in the code regarding the implicit method step shows the steps of the algorithm in the right order, but with wrong arguments in the function calls.
What you do with h is not recognizable. One could guess that the task is to explore the results of the method for a collection of step sizes. This means that the h for each integration run is constant, halving the step size and increasing the step number for the next integration run.

fminsearch error: DOUBLE cannot convert the input expression into a double array

I am encountering a problem during an optimization exercise. I am trying to use fminsearch() in matlab to solve it. The following error is generated when the code is run:
The following error occurred converting from sym to double: Error
using symengine (line 59) DOUBLE cannot convert the input expression
into a double array. If the input expression contains a symbolic
variable, use VPA.
Error in fminsearch (line 190) fv(:,1) = funfcn(x,varargin{:});
Error in Optimization (line 22) sol2 = fminsearch(J, x0);
The script I use can be seen below. The f is the minimization problem where g1 & g2 are constraints. The p is there so that I can turn it into for loop later.
syms x1 x2 x3 x4;
syms p;
f = x1.^4 - x3.^2 + x1*x3 + x2*x4 - 5*x2 + 3;
g1 = x1.^2 + x2.^2 + x3.^2 + x4.^2 - 1;
g2 = x1 + x3 - 1;
x0 = [2 2 2 2];
p = 3;
J = #(x1,x2,x3,x4) sym(f + p * g1.^2 + g2.^2);
sol2 = fminsearch(J, x0);
This Stackoverflowpost has the same problem but in another perspective.According to this post it might be a problem with allocating the in a valid way. I tried a few different ways to solve my problem. I have tried matlabFunction() and putting the function in a seperate file.
If the input expression contains a symbolic variable, use the VPA function instead?
Thanks in advance for the help.
fminsearch is designed for numerical minimization. There is little point in using symbolic math in this case (it can be used, but it will be slower, complicate your code, and the results will still be in double precison). Second, if you read the documentation and look at the examples for fminsearch, you'll see that it requires a function that takes a single vector input (as opposed to four scalars in your case). Here's how you could rewrite your equations using anonymous functions:
f = #(x)x(1).^4 - x(3).^2 + x(1).*x(3) + x(2).*x(4) - 5*x(2) + 3;
g1 = #(x)x(1).^2 + x(2).^2 + x(3).^2 + x(4).^2 - 1;
g2 = #(x)x(1) + x(3) - 1;
x0 = [2 2 2 2];
p = 3;
J = #(x)f(x) + p*g1(x).^2 + g2(x).^2;
sol2 = fminsearch(J, x0)
this returns
sol2 =
0.149070165097281 1.101372214292880 0.326920462283209 -0.231885482601008
Using symbolic math and subs:
syms x1 x2 x3 x4;
f = x1^4 - x3^2 + x1*x3 + x2*x4 - 5*x2 + 3;
g1 = x1^2 + x2^2 + x3^2 + x4^2 - 1;
g2 = x1 + x3 - 1;
x0 = [2 2 2 2];
p = sym(3);
J = #(X)subs(f + p*g1^2 + g2^2,[x1 x2 x3 x4],X);
sol2 = fminsearch(J, x0)
which returns identical results.

How can I get MATLAB to numerically solve a particularly nasty system of equations?

I have four parameters, t1,t2, theta1, and theta2. I want to find a solution (there can potentially be infinitely many) to the following system of equations:
t1*cos(theta1) + t2*cos(theta2) + 2*t1*t2*sin(theta1 + theta2) = t1*sin(theta1) + t2*sin(theta2) + 2*t1*t2*cos(theta1 + theta2) + 1
t1*cos(theta1) + t2*cos(theta2) + t1*sin(theta1) + t2*sin(theta2) = 2*t1*t2*sin(theta1 + theta2) + 2*t1*t2*cos(theta1 + theta2) + 1
2*t1^2*t2^2 + sin(theta1)*t1*t2^2 + sin(theta1 + theta2)*t1*t2 + 1 = sin(theta2)*t1^2*t2 + t1^2 + sin(theta1)*t1 + t2^2
Since there are more parameters than equations, we have to impose further restrictions to identify a specific solution to this system. Right now I don't really care which solution is picked, as long as it isn't trivial (like all the variables equaling zero).
My current method is to set the third equation to 0.5, solving for t1 and t2 in terms of theta1 and theta2, and substituting these expressions into the first two equations to solve for theta1 and theta2. However, MATLAB is trying to do this symbolically, which is extremely time-consuming. Is there a way I can get some approximate solutions to this system? I can't exactly plot the surfaces represented by these equations and look at their intersection, because each side of the equations involves more than two parameters, which means it can't be visualized in three dimensions.
You can use fsolve to do this.
First, you need to create anonymous functions for the residuals of the three equations. I'm going to create a vector x where x = [t1; t2; theta1; theta2]. That makes the residuals:
r1 = #(x) x(1)*cos(x(3)) + x(2)*cos(x(4)) + 2*x(1)*x(2)*sin(x(3) + x(4)) - (x(1)*sin(x(3)) + x(2)*sin(x(4)) + 2*x(1)*x(2)*cos(x(3) + x(4)) + 1);
r2 = #(x) x(1)*cos(x(3)) + x(2)*cos(x(4)) + x(1)*sin(x(3)) + x(2)*sin(x(4)) - (2*x(1)*x(2)*sin(x(3) + x(4)) + 2*x(1)*x(2)*cos(x(3) + x(4)) + 1);
r3 = #(x) 2*x(1)^2*x(2)^2 + sin(x(3))*x(1)*x(2)^2 + sin(x(3) + x(4))*x(1)*x(2) + 1 - (sin(x(4))*x(1)^2*x(2) + x(1)^2 + sin(x(3))*x(1) + x(2)^2);
Then, we need to make a single function that is a vector of those three residuals, since fsolve tries to get this vector to be zero:
r = #(x) [r1(x); r2(x); r3(x)]
Now, we call fsolve. I picked an arbitrary starting point:
>> [x, fval, exitflag] = fsolve(r, [0.5,0.5,pi/4,pi/4])
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
> In fsolve at 287
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
x =
0.9654 0.5182 0.7363 0.7344
fval =
1.0e-10 *
-0.0090
0.2743
-0.0181
exitflag =
1
You can ignore the warning. x is the four values you were looking for. fval is the value of the residuals. exitFlag == 1 means we found a root.

Solving systems of equations, receiving z and z1 in solution?

I passed the following nonlinear system to Matlab:
2(x−p1)+2(xy−p3)y = 0
2(y−p2)+2(xy−p3)x = 0
and used syms to find solution for x and y symbolically but I got:
sol.x
ans =
(p1^3 + p3*p1^2*z1 + p1*z1^4 - 1.0*p2*p1*z1^3 + p1*z1^2 - 1.0*p2*p1*z1 + p3*z1^3 - 1.0*p2*p3*z1^2 + p3*z1 - 1.0*p2*p3)/(p1^2 + p3^2)
and
sol.y
ans =
z1
where z1 = RootOf(z^5 - p2*z^4 + 2*z^3 - z^2*(2*p2 - p1*p3) + z*(p1^2 - p3^2 + 1) - p1*p3 - p2, z)
I dont understand where z come from? what is z?
Your y solution is expressed in terms of roots of a polynomial in z that depends on your equation's parameters also.
To show why is difficult to answer your question in the present form, please allow me to rephrase it: the numbers I'm looking for are the roots of an equation f(z) = 0; now, where z comes from? :-)

Matlab: How to solve the system of nonlinear equations with additional parameters?

I would like to create a function that finds the parameters p and q of Bass diffusion model, given the data of two time periods.
The model (equation) is the following:
n(T) = p*m + (q-p)*n(T-1) + q/m*n(T-1)^2
where
n(T) = number of addoptions occuring in period T
n(T-1) = number of cumulative adoptions that occured before T
p = coefficient of innovation
q = coefficient of imitation
m = number of eventual adopters
for example if m = 3.000.000
and the data for the years below is the following:
2000: n(T) = 820, n(T-1) = 0
2005: n(T) = 25000, n(T-1) = 18000
then the following equation system has to be solved (in order to determine the values of p and q):
p*m + (q-p)*0 + q/3.000.000 * 0^2 == 820
p*m + (q-p)*18000 + q/3.000.000 * 18000^2 == 25000
By following Matlab documentation I tried to create a function Bass:
function F = Bass(m, p, q, cummulativeAdoptersBefore)
F = [p*m + (q-p)*cummulativeAdoptersBefore(1) + q/m*cummulativeAdoptersBefore(1).^2;
p*m + (q-p)*cummulativeAdoptersBefore(2) + q/m*cummulativeAdoptersBefore(2).^2];
end
Which should be used in fsolve(#Bass,x0,options) but in this case m, p, q, cummulativeAdoptersBefore(1), and cummulativeAdoptersBefore(2) should be given in x0 and all variables would be considered as unknown instead of just the latter two.
Does anyone know how to solve the system of equations such as above?
Thank you!
fsolve() seeks to minimize the function you supply as argument. Thus, you have to change your equations to
p*m + (q-p)*0 + q/3.000.000 * 0^2 - 820 == 0
p*m + (q-p)*18000 + q/3.000.000 * 18000^2 - 25000 == 0
and in Matlab syntax
function F = Bass(m, p, q, cumulativeAdoptersBefore, cumulativeAdoptersAfter)
F = [p*m + (q-p)*cumulativeAdoptersBefore(1) ...
+ q/m *cumulativeAdoptersBefore(1).^2
- cumulativeAdoptersAfter(1);
p*m + (q-p)*cumulativeAdoptersBefore(2) ...
+ q/m *cumulativeAdoptersBefore(2).^2
- cumulativeAdoptersAfter(2)];
end
Note: There is a typo in your Bass function (multiplication instead of sum).
Now you have a function, which takes more parameters than there are unkowns.
One option is to create an anonymous function, which only takes the unknowns as arguments and to fix the other parameters via a closure.
To fit the unkowns p and q, you could use something like
cumulativeAdoptersBefore = [0, 1800];
cumulativeAdoptersAfter = [820, 25000];
m = 3e6;
x = [0, 0]; %# Probably, this is no good starting guess.
xopt = fsolve(#(x) Bass(m, x(1), x(2), cumulativeAdoptersBefore, cumulativeAdoptersAfter), x0);
So fsolve() sees a function taking only a single argument (a vector with two elements) and it also returns a vector value.