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!