MATLAB: How to solve equations with symsum inside

equationssymsum

I need to solve a two-dimensional system of equations. I have a column-vector, named here "SpreadCLPbp" with N=length(SpreadCLPbp)=500.
I want to solve:
0 = sum_{i=1}^{N}{(-1/mu2 +exp(mu2*delta)/mu2)*(SpreadCLPbp(k)-(-mu1/mu2+(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)))/(mu1*sigma/(2*mu2^2)-sigma^2/mu2*(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)+sigma^2/mu2*(mu1/(2*mu2)+SpreadCLPbp(k))*exp(2*mu2*delta))}
0 = sum_{k=1}^{N}{(mu1/mu2+delta*exp(mu2*delta)*(mu1/mu2+SpreadCLPbp(k))-exp(mu2*delta)*mu1/mu2)*(SpreadCLPbp(k)-(-mu1/mu2+(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)))/(mu1*sigma/(2*mu2^2)-sigma^2/mu2*(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)+sigma^2/mu2*(mu1/(2*mu2)+SpreadCLPbp(k))*exp(2*mu2*delta))}
I tried to do the following
syms k mu1 mu2;
S1 = symsum((-1/mu2 +exp(mu2*delta)/mu2)*(SpreadCLPbp(k)-(-mu1/mu2+(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)))/(mu1*sigma/(2*mu2^2)-sigma^2/mu2*(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)+sigma^2/mu2*(mu1/(2*mu2)+SpreadCLPbp(k))*exp(2*mu2*delta)),1,N);
S2 = symsum((mu1/mu2+delta*exp(mu2*delta)*(mu1/mu2+SpreadCLPbp(k))-exp(mu2*delta)*mu1/mu2)*(SpreadCLPbp(k)-(-mu1/mu2+(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)))/(mu1*sigma/(2*mu2^2)-sigma^2/mu2*(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)+sigma^2/mu2*(mu1/(2*mu2)+SpreadCLPbp(k))*exp(2*mu2*delta)),1,N);
S = solve([S1 == 0, S2 == 0],[mu1, mu2])
But I get the following error
Error using sym/subsindex (line 732)
Invalid indexing or function definition. When defining a function, ensure that the arguments are symbolic variables and
the body of the function is a SYM expression. When indexing, the input must be numeric, logical, or ':'.
Error in analisisdatoscir (line 42)
S1 = symsum((-1/mu2
+exp(mu2*delta)/mu2)*(SpreadCLPbp(k)-(-mu1/mu2+(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)))/(mu1*sigma/(2*mu2^2)-sigma^2/mu2*(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)+sigma^2/mu2*(mu1/(2*mu2)+SpreadCLPbp(k))*exp(2*mu2*delta)),1,N);
If someone could help me I would be very grateful. Watch for answers. Greetings.

Best Answer

  • You have three symbolic variables; you should be naming your variable of summation specifically.
  • In MATLAB, symsum is for the case where the variable of summation is used in the calculation, not to add a definite list of terms. You should create a vector of terms and add them with sum(), or create a total using a loop. Using arrayfun() can help:
t1 = arrayfun(@(k) ((-1/mu2 +exp(mu2*delta)/mu2)*(SpreadCLPbp(k)-(-mu1/mu2+(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)))/(mu1*sigma/(2*mu2^2)-sigma^2/mu2*(mu1/mu2+SpreadCLPbp(k))*exp(mu2*delta)+sigma^2/mu2*(mu1/(2*mu2)+SpreadCLPbp(k))*exp(2*mu2*delta)), 1:N, 'Uniform', 0);
S1 = sum( horzcat(t1{:}) );
  • solve() is for analytic solutions, when you need the result to be the formula for the solution. Your chances of being able to find the exact formula for this with your available resources are pretty minimal. You should consider using vpasolve() to get a symbolic numeric solution; or you should consider using matlabFunction() and fsolve() to get a pure numeric solution.