fsolve and symbolic non-linear system of equations
Show older comments
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)
John D'Errico
on 27 May 2016
0 votes
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.
Walter Roberson
on 27 May 2016
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
LuC
on 30 May 2016
Walter Roberson
on 30 May 2016
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.
Walter Roberson
on 31 May 2016
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.
LuC
on 1 Jun 2016
Walter Roberson
on 1 Jun 2016
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.
LuC
on 3 Jun 2016
Walter Roberson
on 3 Jun 2016
Edited: Walter Roberson
on 3 Jun 2016
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.
Categories
Find more on Time-Domain Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!