How to make a loop until a function has a nonzero limit?
12 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (2 件)
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 件のコメント
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 件のコメント
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 Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!