fsolve syms char double

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 Comments

What error message do you get ?
The error I get is about matlab's uncapability of handling the variable system2 with mat2str since it's not numerical.In the main I forgot to paste syms x theta and as well z.
??? Error using ==> mat2str at 56
Input matrix must be numeric.
Error in ==> equilibrium4 at 17
F=mat2str(vpa(system2))
Any hint on how to solve the problem ?
Does "system2" contain any variable names or is it completely numeric? If it is completely numeric, then change the vpa() to double()
As you can see from the function script it originally contains symbolic variable z(i)=[x;theta].Then I substitute it with the 'd' that should be a string.But I can't exactly figure out what I'm dealing with.If you could help me to understand how to handle these different types of variables I'd be very thankful
I tried with double but it returns the following error
??? Error using ==> sym.double at 29 DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead.
Error in ==> equilibrium4 at 17
F=mat2str(double(system2))
In another script I tried also function [zstatic,residuals]=equilibrium6(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=char(vpa(system2))
F=F(7:end)
h=str2func( ['@(x)' F]); [zstatic,residuals]=fsolve(h,zeros(n,1))
and it returns
??? Undefined function or method '@(y)([[100.0*y(1) - 9.3452801322378782344912906410173],[(-4.905)*sin(y(2))]])' for input arguments of type 'double'.
Error in ==> fsolve at 249 fuser = funfcn{3}(x,varargin{:});
Error in ==> equilibrium6 at 21 [zstatic,residuals]=fsolve(h,zeros(n,1)) Error in ==> main at 54 [zstatic,residuals]=equilibrium6(K,Q,z) Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
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))
?
Because it returns
??? Input argument "y" is undefined.
Error in ==> equilibrium7 at 7
system2=subs(system2,z(i),y(i))
Error in ==> main at 54
[zstatic,residuals]=equilibrium7(K,Q,z)
You are only passing three parameters to the routine even though you defined the routine with 4 parameters.
I know but that's the strange way fsolve should work.You pass the variable parameter as an input without explicitly defining it
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.
My intention is to substitute to symbolic variables the i-th element of vector y.Vector y is the vector for which system2 is gonna be solved using fsolve, therefore isn't defined, but simply called as an input.Is this possible or I'm going to get stuck?
Ah, finally something I can work with...

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 4 May 2013
Edited: Walter Roberson on 4 May 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 Comments

Thanks a lot for the attention, but there's still one thing I can't get, what should I substitute to vars or what I need to define as vars?
'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.
I called the function from the main without modifying anything in the script but it still returns an error (I'm running MATLAB R2008b on Os X)
??? Error using ==> sym.matlabFunction at 55 Argument 'vars' failed validation isvars.
Error in ==> equilibrium11 at 3 h = matlabFunction(system2, 'vars', num2cell(z))
Error in ==> main at 54 [zstatic,residuals]=equilibrium11(K,Q,z)
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.
It gives exactly the same error on isvars
syms x y
matlabFunction(x+y, 'vars', {x, y})
??? Error using ==> sym.matlabFunction at 55
Argument 'vars' failed validation isvars.
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.
MATLAB R2008b version 7.7.0.741 for Os X running on Snow Leopard
Now it works, I've posted below the input and the output if for you is of any interest
syms x y
matlabFunction(x+y, 'vars', [x, y])
ans =
@(x,y)x+y
Okay then,
varscell = num2cell(z);
RealH = matlabFunction(system2, 'vars', z);
VecH = @(x) RealH(varscell{:});
[zstatic, residuals] = fsolve(VecH, zeros(size(system2,1),1))
A new problem showed up:
??? Undefined function or method 'full' for input arguments of type 'sym'.
Error in ==> trustnleqn at 29
Fvec = full(Fvec);
Error in ==> fsolve at 374
[x,FVAL,JACOB,EXITFLAG,OUTPUT,msg]=...
Error in ==> equilibrium11 at 6
[zstatic, residuals] = fsolve(VecH, zeros(size(system2,1),1))
Error in ==> main at 54
[zstatic,residuals]=equilibrium11(K,Q,z)
It is returned by calling the main with the following function
function [zstatic,residuals]=equilibrium11(K,Q,z)
system2=K*z-Q
varscell = num2cell(z);
RealH = matlabFunction(system2, 'vars', z);
VecH = @(x) RealH(varscell{:});
[zstatic, residuals] = fsolve(VecH, zeros(size(system2,1),1))
Could you show
size(z)
functions(VecH)
functions(RealH)
and the result of
VecH(ones(1,length(z))
Here it is what you asked me
>> size(z)
ans =
2 1
>> functions(VecH)
ans =
function: '@(x)RealH(varscell{:})'
type: 'anonymous'
file: ''
workspace: {[1x1 struct]}
>> functions(RealH)
ans =
function: [1x88 char]
type: 'anonymous'
file: [1x70 char]
workspace: {2x1 cell}
>> VecH(ones(1,length(z)))
ans =
100*x - 2630462507576213/281474976710656
-(981*sin(theta))/200

Sign in to comment.

Categories

Asked:

on 3 May 2013

Community Treasure Hunt

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

Start Hunting!