How to make a loop until a function has a nonzero limit?

조회 수: 1 (최근 30일)
Andy
Andy 2012년 10월 14일
I'm making an application for L'hopitals rule so the condition has to be until one of the functions has derived enough to have a nonzero limit. For this application, I have to use a while loop so here's what I got already.
while (limit(f,x,0)==0 limit(g,x,0)==0)
a=diff(f(x));
b=diff(g(x));
end;
y=(limit(f,x,0))/(limit(g,x,0))
My problem is since I need to assign variables to differentiate the two functions and the variables cannot be the same as the ones I assigned to the functions, how can I tell the while loop to continue to derive the functions when the derived functions has a different variable?

답변 (2개)

Matt J
Matt J 2012년 10월 14일
If you're just trying to avoid overwriting f and g, why not just make prior copies of them:
a=f;
b=g;
while (limit(a,x,0)==0 limit(b,x,0)==0)
a=diff(a(x));
b=diff(b(x));
end;
y=(limit(a,x,0))/(limit(b,x,0))
  댓글 수: 2
Andy
Andy 2012년 10월 14일
well I can't overwrite any variable according to the errors I'm getting
Matt J
Matt J 2012년 10월 14일
Show the errors (by COPY/PASTE only)

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


Star Strider
Star Strider 2012년 10월 14일
편집: Star Strider 2012년 10월 14일
I am not certain I completely understand your problem, but if you want to continue to differentiate your functions until you get a finite result, I suggest this approach:
syms a b c d f g x
f(x) = sin(x)
g(x) = 1 - exp(x)
c = f(x)
d = g(x)
k1 = 1;
while (limit(c,x,0)==0) && (limit(d,x,0)==0)
a=diff(c(k1,:));
b=diff(d(k1,:));
k1 = k1 + 1
c(k1,:) = a
d(k1,:) = b
end;
y = (limit(c(k1,:),x,0))/(limit(d(k1,:),x,0))
Vectors c and d store the intermediate results, so you do not overwrite your original functions.
  댓글 수: 2
Andy
Andy 2012년 10월 14일
I forgot to mention but the f and g in this function are function handles
Star Strider
Star Strider 2012년 10월 14일
편집: Star Strider 2012년 10월 14일
I could only get this to work in the Symbolic Math Toolbox. I assumed that when you referred to limit in your code snippet, you were using it.
I just now tried the code I posted with f and g redefined as:
f = @(x) sin(x)
g = @(x) cos(x) - exp(x)
and it worked just as well as with the original function definitions.
NOTE: I'm using 2012b.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by