Sum of series (symsum) in Simulink

9 views (last 30 days)
Johannes Poser
Johannes Poser on 25 Aug 2022
Commented: Paul on 26 Aug 2022
Hi guys,
I'm trying to solve an equation in Simulink. Solving it in Matlab works niceley but I don't know how to implement it in Simulink. It goes as follows:
k = 5;
x80 = 4;
syms mu n;
eqn = 1 - exp(-x80out/mu)*symsum((1/factorial(n))*(x80out/mu)^n, n, 0, (k-1)) == 0.8;
mu = solve(eqn, mu);
Does anybody know how to implement the sum of series in Simulink?
Alternatively I thought about using a Matlab function but it doesn't allow to introduce the variables via "syms mu n".
Thanks in advance:)
  2 Comments
Paul
Paul on 25 Aug 2022
Hi Johannes,
What is x80out?
Are k and x80 (or x80out) to be computed (and changing) in the Simullnk model from which mu should be computed as the simulation is running?
Or are k and x80 (x80out) known a priori, and then mu only needs to be computed once before the simulation starts to execute?
Johannes Poser
Johannes Poser on 25 Aug 2022
Hi Paul,
x80out is supposed to be x80 = 4.
x80 and k are both changing during the simulation and therefore mu too.

Sign in to comment.

Accepted Answer

Paul
Paul on 26 Aug 2022
Edited: Paul on 26 Aug 2022
I was able to get a workable soluion using only Simulink blocks by setting up the equation and solving for mu with an Algebraic Constraint block. The symsum part of the equation can be implemented in a For Iterator Subsystem It's much easier to use a Matlab Function block to really do the heavy lifting using @Walter Roberson code from this comment
function balance = fcn(k,x80out,mu)
n = 0 : k-1;
inner = sum( (1./factorial(n)) .* (x80out/mu).^n );
balance = 1 - exp(-x80out/mu) .* inner - 0.8;
end
  2 Comments
Johannes Poser
Johannes Poser on 26 Aug 2022
Oh maaaaaan this is amazing I can't believe it!
Many thanks to both of you. I think I would have never figured that out without you. Im truly grateful and it works so nicely.
Paul
Paul on 26 Aug 2022
HTH.
I didn't test if that code works when k varies with time. If not, just covert the computation of inner to a simple for loop.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 25 Aug 2022
Nothing in the Symbolic Toolbox is supported for code generation, including not for Simulink Rapid Acceleration purposes.
You could try using coder.external coder.extrinsic -- if you do remember that symbolic variables are not permitted to be signals, so remember to double()
But you should probably instead switch to using an anonymous function and fzero, or perhaps a private function, along the lines of
mu = 0;
mu = fsolve(@mu_eqn, 1);
function balance = mu_eqn(mu)
k = 5;
x80out = 4;
balance = 1 - exp(-x80out/mu)*symsum((1/factorial(n))*(x80out/mu)^n, n, 0, (k-1)) - 0.8;
end
  4 Comments
Paul
Paul on 26 Aug 2022
Edited: Paul on 26 Aug 2022
The following might work, I didn't try it.
Pass k and x80 into a Matlab Function block. The code would look like this
function myfunc(x80,k)
coder.extrinsic('solution')
mu = solution(x80,k)
end
Define a garden variety Matlab funtion in an m-file, call it solution.m.
function mu = solution(dx80,dk)
x80 = sym(dx80);
k = sym(dk);
syms mu n;
eqn = 1 - exp(-x80out/mu)*symsum((1/factorial(n))*(x80out/mu)^n, n, 0, (k-1)) == 0.8;
mu = double(solve(eqn, mu));
end
If that doesn't work, or is too slow, solution .m can be modified to use fsolve. I don't see a way to call fsolve directly from a Matlab Function block in this case.
Johannes Poser
Johannes Poser on 26 Aug 2022
You guys are awesome, I can't thank you enough

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!