Determining symbolic function values without using matlabFunction
显示 更早的评论
Hello all, I am trying to evaluate a symbolic matrix at a certain point. The symbolic functions are as follows:
clc, clear all
x=sym("x",[1 3]);
f(1,1)=x(1)^2 + 2*x(2) +x(3);
f(2,1)=2*x(1) + x(2)^3 + x(2)^2;
f(3,1)=x(1) + x(2) + 2*x(3)^2;
% Finding the jacobian
for i=1:length(f)
for j=1:length(f)
jac(i,j)=diff(f(i), x(j));
end
end
Now, I would like to evaluate the jac at x=[1 2 3].
x=[1 2 3]; % point where jac will be evaluated
It can be done by several ways.
First method:
fv1=tic;
jac_n=matlabFunction(jac);
x_n=num2cell(x);
J1=jac_n(x_n{:})
t_fv1=toc(fv1)
time taken = 0.2255 s.
Second method:
fv2=tic;
J2=[2*x(1) 2 1; 2 3*x(2)^2 2*x(3); 1 1 2] % jac matrix is written manually
t_fv2=toc(fv2
time taken = 4.2230e-04 s
First method takes way longer time than the second method. Also, the first method may not work if all x variables are not present in jac. Now, I want to use the second method as it takes very less time. But I have to write the expressions of jac manually. Is there any way that I can write the expressions of jac automatically? Also, it would be great if anybody can suggest any method faster than those two mentioned. Thank you.
采纳的回答
更多回答(1 个)
Walter Roberson
2020-5-2
without having done the x=[1 2 3];
xv = [1 2 3];
subs(jac, x, xv)
time about 0.0157 on my system.
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Algebra 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!