Computing a Jacobian Numerically using 5pt stencil approximation
显示 更早的评论
Hi guys! I am trying to compute a jacobian numerically using a 5-pt stencil approximation. my array F contains two functions:
x = [x1;x2];
f = @(x1,x2) func1(x);
g = @(x1,x2) func2(x);
%Assigning functions to an array
F(1,1) = {f};
F(2,1) = {g};
Then, I am trying to compute my jacobian but am not getting proper results.
function [J,h] = jacob(F,x)
[n,m] = size(x);
h = zeros(n,1);
%Initialize Jacobian
J = zeros(n,n);
%Numerical computation of Jacobian using 5-pt stencil approximation
for i = 1:n
for j = 1:m
%If i == j, h takes the value of the step size
if i == j
h(i) = 1e-3;
end
J(i,j) = (F{i,1}(x(j)+2*h(j)) + 8*F{i,1}(x(j)+h(j)) - 8*F{i,1}(x(j)-h(j)) + F{i,1}(x(j)-2*h(j)))/(12*h(j));
h(j) = 0;
end
end
end
3 个评论
Matt J
2017-10-27
I am trying to compute my jacobian but am not getting proper results.
as demonstrated by...?
Walter Roberson
2017-10-27
The question is how you know you are getting results that are not proper. What should we be looking at? If we were to make a change to your code in hopes of fixing the problem, then how would we know if we had succeeded ?
Cassandra Athans
2017-10-27
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!