How to use three- line loop with disp to find derivative

I want to find the first five derivatives of f(x)=x((2x+1)^1/2), Using the three-line for loop wih disp.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 2 月 12 日

0 投票

syms x real
f = x.*(2*x+1).^(1/2)
df = arrayfun(@(i1)diff(f,x,i1),1:5,'un',0);
out = cat(1,df{:})
:)
out = sym('out',[5, 1]);
x = sym('x');
for i1 = 1:5
out(i1) = diff(x.*(2*x+1).^(1/2),x,i1);
end

1 件のコメント

Dhurgham Kadhim
Dhurgham Kadhim 2012 年 2 月 13 日
The answer is correct, but it is not the for-end loop's method.

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2012 年 2 月 13 日

0 投票

You cannot find the derivative using a FOR-loop. As far as I can see, you only can use forward, backward or central differences to calculate an approximation of the derivative.
It is not clear what a "three-line for loop" is any what "with disp" exactly mean. It seems like you got a strange homework assignment.
G A
G A 2012 年 2 月 14 日

0 投票

syms y x
y=x*(2*x+1)^(1/2);
f{1}=diff(y,x);
for n=2:5, f{n}=diff(f{n-1},x);end
f{:} %is this what you mean disp?

2 件のコメント

Dhurgham Kadhim
Dhurgham Kadhim 2012 年 2 月 14 日
disp means display the answer. I evulated your steps but did not give me the derivative. Thanks
G A
G A 2012 年 2 月 14 日
strange... you can just copy this code to the matlab window and press 'enter'. Do not forget f{:}

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDebugging and Improving Code についてさらに検索

質問済み:

2012 年 2 月 12 日

編集済み:

2013 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by