fsolve and symbolic non-linear system of equations
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (2)
John D'Errico
le 27 Mai 2016
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
le 27 Mai 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 commentaires
Walter Roberson
le 3 Juin 2016
Modifié(e) : Walter Roberson
le 3 Juin 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.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!