fsolve and symbolic non-linear system of equations

Hello,
I have a function that is defined in the following way:
function f = ts_7( x, xdata, ydata)
syms x D C B E A
g = D * sin(C * atan( B*x - E * (B*x - atan(B*x)))) + A;
f = cell(1,5);
f{5} = taylor(g, x, 'Order',1, 'ExpansionPoint',7); % x^0
f{1} = taylor(g, x, 'Order',2, 'ExpansionPoint',7) - f{5}; % x^1
f{2} = taylor(g, x, 'Order',3, 'ExpansionPoint',7) - f{1}; % x^2
f{3} = taylor(g, x, 'Order',4, 'ExpansionPoint',7) - f{2}; % x^3
f{4} = taylor(g, x, 'Order',5, 'ExpansionPoint',7) - f{3}; % x^4
for i = 1 : 5
f{i} = real(subs(f{i}, {'B', 'C', 'D', 'E', 'A'}, {x(1), x(2), x(2), x(3), x(4), x(5)}));
end
and my scripts is
opts = optimoptions('fsolve','InitDamping',0.005,'Algorithm','levenberg-marquardt');
x0 = [-1.3, 1.4, 4000, 0.12, 9]
f = @(x)ts_7;
x = fsolve(@ts_7, x0, opts);
Error:
Error using subsref
Index exceeds matrix dimensions.
Error in sym/subsref (line 771)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in ts_7 (line 29)
f{i} = real(subs(f{i}, {'B', 'C', 'D', 'E', 'A'}, {x(1), x(2), x(2), x(3), x(4), x(5)}));
Anyone has an idea how to solve it? IS there an equivalent of
matlabFunction for cells
Thanks in advance!
EDIT: I changed the code because I realised I cannot assign syms to a vector, i.e.
f(5) = taylor(g, x, 'Order',1, 'ExpansionPoint',7); % x^0
gives an error.

Answers (2)

This is a basic nonlinear least squares problem. Use lsqnonlin or lsqcurvefit to solve it.
There is no need at all to do ANY symbolic computations.

1 Comment

Can you explain what you mean that there is no need to do any symbolic computations? How otherwise could I find a Taylor expansion of g(x)?

Sign in to comment.

taylor returns a formula . You then need to subs() a particular numeric value in to the formula.
Your ts_7 is defined for three inputs but does not use the second or third variables.
Your ts_7 is defined using x as the first parameter, but it then uses
syms x
which is equivalent to
x = sym('x');
which overwrites the parameter x with sym('x'). If you want to be able to substitute the parameter in to your formula, you need to rename one of the two. For example
function f = ts_7(X)
syms x ...
f(1) = .....
...
f = double( subs(f, x, X) ); %evaluate at input x and convert to double

8 Comments

There are no enough input data. Should I replace D, C, B, E, A with a vertor s.t. m(1) = D, m(2) = C, and so on?
Are those values available at the time you call ts_7 ? If so then pass them in; it does not matter much whether you use individual variables or a vector.
LuC
LuC on 31 May 2016
Edited: LuC on 31 May 2016
No, those are the parameters I want to find with fsolve. How can I transform symbolic values into vectors? As far as I know fsolve cannot solve symbolic equations, maybe solve could work.
I do not understand how your xdata and ydata are used. They do not appear in the body of the code you posted.
solve() is not likely to work unless you have several tens of gigabytes of memory, far more than I do.
vpasolve() might work -- but only if you have an actual value to substitute in for x.
I used xdata and ydata for another function called within this function but I commented it out as I first have to find the coefficients A, B, C, D and E and then call the second function that uses xdata and ydata.
As the values of A, B, C, D, E are not going to change with the input to ts_7, you should calculate them once outside of ts_7, if you can calculate them symbolically. On the other hand, my attempt to solve symbolically turned out to need a lot of memory and time, which suggests that you are going to need to use particular x values (from the input) and that you are going to need to use vpasolve() .
One problem with vpasolve() however is that if the system has multiple solutions (which yours does because of the trig functions) then you do not know which of the solutions you are going to get.) vpasolve() works better if you can give ranges for the variables, which can reduce the problem of multiple solutions.
for vpasolve I get an error 'symbolic parameters are not allowed in non polynomial equation'
Here.
Example run
[a, b, c, d, e] = ts_7(1.2345)
Note: there are many different answers. I set it up to use a random starting point, so expect a different result every time.

Sign in to comment.

Asked:

LuC
on 27 May 2016

Edited:

on 3 Jun 2016

Community Treasure Hunt

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

Start Hunting!