Solve non-linear system of equations with variable number of unknowns

Hi everyone, I need some help. I have written a script to form system of nonlinear equations using the symbolic toolbox (because the equation forms are not fixed and dependant on certain parameters) and I need to solve them. I know how to solve a system of nonlinear equations with multiple unknowns, by parameterising the function as follows:
matfun=matlabFunction( symbolic_eqn );
funMiddleMan = @(f) matfun(f(1),f(2),f(3),f(4),f(5),f(6),f(7),f(8),f(9),f(10),...
f(11),f(12)); % the function has 12 unknowns
x0=zeros(1,12);
[soln_fs,fval_fs]=fsolve(funMiddleMan,x0);
However, for the case where I wish to vary the number of unknowns in "symbolic_eqn", what should I do do make this work? For now, the only idea I can think of is by writing an inelegant list of conditional statements of the form:
switch num_unknowns
case N
funMiddleMan = @(f) matfun(f(1),f(2),...,f(N)); %for N number of variables
[...]
end
Would you please guide me on how I can solve this problem?
Thank you for your help and time.

1 Comment

If it's not possible to do so and I need to use a list of conditional statements please do let me know. Thank you

Sign in to comment.

 Accepted Answer

%{
matfun = matlabFunction( symbolic_eqn, 'vars', [list variables here]);
%}
for example
N = 5;
syms F [1 N]
expr = sum(randn(1,N) .* F);
f1 = matlabFunction( expr, 'vars', F)
f1 = function_handle with value:
@(F1,F2,F3,F4,F5)F1.*3.599379160893824e-1-F2.*1.002299459586992-F3.*1.613867501380812e-1-F4.*1.083541721318095+F5.*2.742004284674245e-1
But most of the time it is easier to instead
f2 = matlabFunction( expr, 'vars', {F} )
f2 = function_handle with value:
@(in1)in1(:,1).*3.599379160893824e-1-in1(:,2).*1.002299459586992-in1(:,3).*1.613867501380812e-1-in1(:,4).*1.083541721318095+in1(:,5).*2.742004284674245e-1
That second use of matlabFunction expects to be passed a row vector of N values, rather than N individual values.

6 Comments

ahhhh that should solve my problem. I will give it a try soon. Thank you!
This line was what I needed to solve the problem. Thank you!
f2 = matlabFunction( expr, 'vars', {F} )
However, I tried to run your code (and some alternatives that I cound think of) but I couldn't get it to work exactly because of this error:
>> N = 5;
syms [1 N]
Error using syms (line 228)
Invalid variable name.
For the sake of learning, I'm interested to know how to make this line work. would you please help me with this? I'm using R2018b. Maybe this is the issue?
Oops sorry for not clarifying. I tried both
N = 5;
syms F [1 N]
and
syms [1 N]
but they both produced errors.
Error using syms (line 228)
Invalid variable name.
F = sym('F', [1 N]);
would still have been needed in your release.
aha! I get it now, I'm familiar with the usage of "sym". Thank you for responding . Merry Christmas to you!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Asked:

on 8 Dec 2020

Commented:

on 25 Dec 2020

Community Treasure Hunt

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

Start Hunting!