fsolve syms char double

4 ビュー (過去 30 日間)
Andrea
Andrea 2013 年 5 月 3 日
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 件のコメント
Andrea
Andrea 2013 年 5 月 4 日
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?
Walter Roberson
Walter Roberson 2013 年 5 月 4 日
Ah, finally something I can work with...

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 5 月 4 日
編集済み: Walter Roberson 2013 年 5 月 4 日
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 件のコメント
Walter Roberson
Walter Roberson 2013 年 5 月 6 日
Could you show
size(z)
functions(VecH)
functions(RealH)
and the result of
VecH(ones(1,length(z))
Andrea
Andrea 2013 年 5 月 6 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by