CAT arguments dimensions are not consistent while using eval command of symbolic jacobian

syms x1 x2
%The Diffenrential Equation
f1= (x1)+(x2*x2);%x1dot Equation
f2=x1-x2; % x2dot Equation
f = [f1, f2];
% Forming the symbolic jacobian
Jac = jacobian(f)
x1=0.0;
x2=1.0;
% eval is used to convert the symbolic jacaobian to numeric jacobian
% Finding the eigen values of the Numeric jacobian
q=eval(Jac)
e=eig(q)
Jac =
[ 1, 2*x2]
[ 1, -1]
q =
1 2
1 -1
e =
1.7321
-1.7321
If i use the above code as a function to calculate the eigen values of the sol of ode45 solver as follows:
Main Program:
clear all
clc;
Tspan =[0 1];
X0=[0.0; 1]; % Initial condition
%solving the differential Equation
[t,x]=ode45(@myfunc,Tspan,X0);
%Plotting the figures
figure(1)
subplot(2,1,1)
plot(t(:,1),x(:,1))
xlabel('t');
ylabel('x1');
grid on
subplot(2,1,2)
plot(t(:,1),x(:,2))
xlabel('t');
ylabel('x2')
grid on
% Finding the eigen values
e=findeig(x)
First Function (Ode function)
function dv=myfunc(t,x,flag)
%The Diffenrential Equation
dv=[ (x(1))+(x(2)*x(2));%x1dot Equation
x(1)-x(2); ]; % x2dot Equation
Second Function to calculate eigen value of x:
function e=findeig(x)
syms x1 x2
%The Diffenrential Equation
f1= (x1)+(x2*x2);%x1dot Equation
f2=x1-x2; % x2dot Equation
f = [f1, f2];
% Forming the symbolic jacobian
Jac = jacobian(f)
x1=x(:,1);
x2=x(:,2);
% eval is used to convert the symbolic jacaobian to numeric jacobian
% Finding the eigen values of the Numeric jacobian
q=eval(Jac)
e=eig(q);
It is giving the following output with error:
x =
0 1.0000
0.0001 0.9999
0.0001 0.9999
0.0002 0.9998
0.0002 0.9998
0.0005 0.9995
0.0007 0.9993
0.0010 0.9990
0.0012 0.9988
0.0025 0.9975
0.0037 0.9963
...... (57x2 array)
Jac =
[ 1, 2*x2]
[ 1, -1]
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> sym.eval at 15
s = evalin('caller',vectorize(map2mat(char(x))));
Error in ==> findeig at 11
q=eval(Jac)
Error in ==> MainProgram at 23
e=findeig(x)
I am expecting the jacobian elemnts has to be replaced with x
for example:for the first row
Jac =
[ 1, 2*1]
[ 1, -1] and has to give eigen value as
1.7321
-1.7321
and so on for all rows of x.why eval is not working for array of elememts

Réponses (1)

Don't use eval on syms. Preferably, don't ever. Better use subs as in
q = double(subs(jacobian(f), {x1, x2}, {x(:,1), x(:,2)}));
(You can of course split that on multiple lines. It's just my personal preference not to.)

7 commentaires

??? Error using ==> horzcat CAT arguments dimensions are not consistent.
Error in ==> sym.subs>@(x)[char(x),','] at 246 s = cellfun(@(x)[char(x) ','],X,'UniformOutput',false);
Error in ==> sym.subs>getNames at 246 s = cellfun(@(x)[char(x) ','],X,'UniformOutput',false);
Error in ==> sym.subs>tryFunctionHandle at 190 xvarnames = getNames(X);
Error in ==> sym.subs>mupadsubs at 137 [G,worked] = tryFunctionHandle(F,X,Y);
Error in ==> sym.subs at 125 G = mupadsubs(F,X,Y);
Error in ==> q = double(subs(Jac , {x1, x2}, {x(:,1), x(:,2)}))
Sir,I am getting error only.
At the point you do that, are x1 and x2 still simple variables, or have you assigned values to them?
syms x1 x2
Jac =[ 1, 2*x2;
x1, -1];
% x1 x2
x= [ 0 , 1;
1 , 2] ;
x1=x(:,1);
x2=x(:,2);
q = double(subs(Jac,{x1, x2},{x(:,1),x(:,2)}))
e=eig(q).
Sir simply u run the code .I want the eigen values for these two operating points. I am expecting the Jac should be [1 2;0 -1] for 1st point and [1 4;1 -1] for second point so i want the eigen values of the matrix as 1,-1 and 2.2361, -2.2361.But i am getting CAT arguments dimensions are not consistent error.Please give ur suggestions.
Actually i want the 1st row 1st element of x i.e (0) to be assigned to x1,similarly 1st row 2nd element of x i.e (1) to be assigned to x2. Jac=[1 2;0 -1]
double(subs(Jac,{OLD},{NEW})).As per this logic the jacobian in terms of symbolic x1,x2 terms has to be replaced with numeric values then eigen values has to be calculated for that numeric jacobian matrix.Th same has to repeated for 2nd row of x i.e [1,2] Jac = [1 4;1 -1] .
As per ur idea,If I remove those statements,i am getting the following error sir .
??? Error using ==> mupadmex Error in MuPAD command: types of matrices don't match [linalg::concatMatrix]
Error in ==> sym.subs>mupadsubs at 149 G = mupadmex('mllib::fullsubs',F.s,X2,Y2);
Error in ==> sym.subs at 125 G = mupadsubs(F,X,Y);
Error in ==> Untitled2 at 11 q = double(subs(Jac,{x1, x2},{x(:,1),x(:,2)})).
waiting 4r ur reply.
If you want the first row first element to be assigned to x1, then do not pass in the entire column x(:,1), only pass in the single element x(1,1)
for i=1:Length q = double(subs(Jac,{x1, x2},{x(i,1),x(i,2)})); e=eig(q); end
Length is the array size .
As per ur idea i used a for loop as above and got the answer.Thanks sir

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by