Question about diff function

조회 수: 1 (최근 30일)
Francisco
Francisco 2014년 12월 9일
편집: Star Strider 2014년 12월 9일
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
Matt J
Matt J 2014년 12월 9일
It is dangerous to use "A" as both a variable name and the name of your function.

댓글을 달려면 로그인하십시오.

답변 (3개)

Star Strider
Star Strider 2014년 12월 9일
To take a numerical derivative most accurately, use the gradient function.
  댓글 수: 2
Francisco
Francisco 2014년 12월 9일
I was looking for an analytic derivative.
Star Strider
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
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.)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 9일
a=[ 2 4 8]
b=diff(a)
b= [4-2 8-4]
a is 1x3 and b is 1x2
  댓글 수: 2
Francisco
Francisco 2014년 12월 9일
I thought diff was http://www.mathworks.com/help/symbolic/diff.html, as in, I was trying to differentiate A. How can I achieve this, then?
Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 9일
B=A(V,h)
C=diff (A (V, h), h);
h = h - B(h+1:end)/C ;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by