fsolve syms char double
Mostrar comentarios más antiguos
Hello I'm writing a program to solve non linear equations of motion and in particular to determine equilibrium position. Therefore defined the matrices of masses dampers and stiffnesses, I'd like to impose Kz=Q where z is a sym array built with symbolic variables.
thus calling a function to solve this problem i have:
In main
m1 = .1; %[kg]
m2 = 1; %[kg]
L = .5; %[m]
k = 100; %[N/m]
c = 3; %[Ns/m]
c_rot = .05*20; %[Nm/rad]
g = 9.81; %[m/s^2]
alpha=pi/3; %[rad]
F0=12; %[N]
z=[x;theta];
%--------------------------------------------------------------------------
% Non linear system
%--------------------------------------------------------------------------
M=[m1 0;
0 m2*L^2+m1*x^2];
C=[c 0; c_rot c];
K=[k 0; 0 0];
Q=[(m1+m2)*g*sin(alpha); m2*g*L*sin(theta)];
%--------------------------------------------------------------------------
% Linear system
%--------------------------------------------------------------------------
Ml=[];
Cl=[];
Kl=[];
Ql=[];
%--------------------------------------------------------------------------
% Static equilibrium position fron non linear
%--------------------------------------------------------------------------
[zstatic,residuals]=equilibrium4(K,Q,z)
And in the function
function [zstatic,residuals]=equilibrium4(K,Q,z,y)
system2=K*z-Q
n=size(system2,1)
for i=1:n
clear a
a='y('
b=num2str(i)
c=')'
d=[a,'',b,'',c]
system2=subs(system2,z(i),d)
end
F=mat2str(vpa(system2))
h= str2func( ['@(x)' F]);
[zstatic,residuals]=fsolve(h,zeros(n,1))
The problem I encountered is the handling of different types of variables.So I ask you if you can provide me any hint about how to solve this problem in order to make the fsolve work.I tried either calling another function as it's used to be done with input functions but It didn't work as well. Thanks for the attention
12 comentarios
Walter Roberson
el 3 de Mayo de 2013
What error message do you get ?
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
Does "system2" contain any variable names or is it completely numeric? If it is completely numeric, then change the vpa() to double()
Andrea
el 4 de Mayo de 2013
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
Is the y(i) constructed as a string intended to make reference to the variable "y" that is in the calling sequence for equilibrium4 ? Why not just
subs(system2, z(i), y(i))
?
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
You are only passing three parameters to the routine even though you defined the routine with 4 parameters.
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
Ummm, is your intention to pass data down to be substituted into the expression that is then solved for? Or is your intention to pass in the string 'y(1)', 'y(2)' and so on?
If you pass in the string 'y(1)' then subs will automatically sym() the string, as if you had used sym('y(1)'). But in MuPAD, round brackets indicate a function call, not indexing, so you are substituting in a call to a function named 'y' with a parameter of 1 . There are a number of circumstances under which that would be equivalent to just happening to name a variable oddly, but there are other circumstances in which it would cause difficulty.
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
Ah, finally something I can work with...
Respuestas (1)
Walter Roberson
el 4 de Mayo de 2013
Editada: Walter Roberson
el 4 de Mayo de 2013
function [zstatic,residuals]=equilibrium4(K,Q,z)
system2=K*z-Q
h = matlabFunction(system2, 'vars', num2cell(z))
[zstatic, residuals] = fsolve(h, zeros(size(system2,1),1))
11 comentarios
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
'vars' is a keyword for matlabFunction(). The value associated with the keyword is num2cell(z) -- that is, the cell array equivalent of your vector of variable names.
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
Could you try this experiment:
syms x y
matlabFunction(x+y, 'vars', {x, y})
and see if it reports the same error? If it does then you might perhaps be using an older version of the symbolic toolbox, and I can tell you how to adjust in that case. If it does not give you that error then the problem might be in what is in z, and I will need to see that.
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
Which MATLAB version are you working with?
Ummm... experiment with
syms x y
matlabFunction(x+y, 'vars', [x, y])
If that does not work, I can probably still do something but it will be slightly messier.
Andrea
el 4 de Mayo de 2013
Walter Roberson
el 4 de Mayo de 2013
Okay then,
varscell = num2cell(z);
RealH = matlabFunction(system2, 'vars', z);
VecH = @(x) RealH(varscell{:});
[zstatic, residuals] = fsolve(VecH, zeros(size(system2,1),1))
Andrea
el 5 de Mayo de 2013
Editada: Walter Roberson
el 6 de Mayo de 2013
Walter Roberson
el 6 de Mayo de 2013
Could you show
size(z)
functions(VecH)
functions(RealH)
and the result of
VecH(ones(1,length(z))
Andrea
el 6 de Mayo de 2013
Categorías
Más información sobre Data Type Identification en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!