Question about diff function
1 次查看(过去 30 天)
显示 更早的评论
I have defined a function as follows
function A = A(V, h)
A = V/h + 2*sqrt(V*pi*h)
end
In another function, I try to call it:
h = h - A (V, h) / diff (A (V, h), h);
But I get the error
Error using / Matrix dimensions must agree.
I am at a loss. Can someone give me a hand?
1 个评论
回答(3 个)
Star Strider
2014-12-9
2 个评论
Star Strider
2014-12-9
编辑:Star Strider
2014-12-9
That requires the Symbolic Math Toolbox.
EDIT —
If you are satisfied with an approximation, you can do a numeric version of the definition of the derivative as an anonymous funciton:
dfdx = @(f,x) (f(x+1E-8)-f(x))./1E-8; % Conventional Derivative
A = @(V,h) V./h + 2*sqrt(V*pi*h);
V = 3;
h = linspace(0.1,10);
dAdh = dfdx(@(h) A(V,h), h);
figure(1)
plot(h, A(V,h), '-b')
hold on
plot(h, dAdh, '-r')
hold off
legend('A(V,h)', 'dA(V,h)/dh')
You may want to experiment with the 1E-8 value (this is the value that most often ‘works’ for me). At the very least, it will provide you with something to experiment with.
Roger Stafford
2014-12-9
h = h - (V/h+2*sqrt(V*pi*h))/(-V/h^2+V*pi/sqrt(V*pi*h));
(Maybe I am old-fashioned, but with elementary functions like this, it's easier to use calculus than to have to mess around with the 'diff' function in my opinion.)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!