matlab 求解 f(x)=x(x+1)(x+2)(x+3)(x+4)...(x+n-2)(x+n-1)(x+n)的导数;
问题的提出
代码求解
clc; clear; close all;
filename=\'how10.txt\';
syms x; % be careful performance issue.
fileID = fopen(filename, \'w\');
str="x";
f=str2sym(str);
fprintf(fileID,\'%s\r\n\',string(f));
result=diff(f,x);
fprintf(fileID,\'%s\r\n\',string(result));
ff=matlabFunction(result);
fprintf(fileID,\'%d\r\n\',1);
fprintf(fileID,\'\r\n\');
for i = 1:10
newstr="*(x+"+int2str(i)+")";
str=str+newstr;
f=str2sym(str);
fprintf(fileID,\'%s\r\n\',string(f));
result=diff(f,x);
fprintf(fileID,\'%s\r\n\',string(result));
ff=matlabFunction(result);
fprintf(fileID,\'%f\r\n\',ff(0));
fprintf(fileID,\'\r\n\');
end
代码输出
x 原函数
1 导函数
1
x*(x + 1) 原函数
2*x + 1 导函数
1.000000
x*(x + 1)(x + 2)
(x + 1)(x + 2) + x*(x + 1) + x*(x + 2)
2.000000
x*(x + 1)(x + 2)(x + 3)
x*(x + 1)(x + 2) + x(x + 1)(x + 3) + x(x + 2)(x + 3) + (x + 1)(x + 2)*(x + 3)
6.000000
x*(x + 1)(x + 2)(x + 3)(x + 4)
x(x + 1)(x + 2)(x + 3) + x*(x + 1)(x + 2)(x + 4) + x*(x + 1)(x + 3)(x + 4) + x*(x + 2)(x + 3)(x + 4) + (x + 1)(x + 2)(x + 3)*(x + 4)
24.000000
x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)
x*(x + 1)(x + 2)(x + 3)(x + 4) + x(x + 1)(x + 2)(x + 3)(x + 5) + x(x + 1)(x + 2)(x + 4)(x + 5) + x(x + 1)(x + 3)(x + 4)(x + 5) + x(x + 2)(x + 3)(x + 4)(x + 5) + (x + 1)(x + 2)(x + 3)(x + 4)*(x + 5)
120.000000
x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)
x(x + 1)(x + 2)(x + 3)(x + 4)(x + 5) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 6) + x*(x + 1)(x + 2)(x + 3)(x + 5)(x + 6) + x*(x + 1)(x + 2)(x + 4)(x + 5)(x + 6) + x*(x + 1)(x + 3)(x + 4)(x + 5)(x + 6) + x*(x + 2)(x + 3)(x + 4)(x + 5)(x + 6) + (x + 1)(x + 2)(x + 3)(x + 4)(x + 5)*(x + 6)
720.000000
x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)
(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6) + x(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 7) + x(x + 1)(x + 2)(x + 3)(x + 4)(x + 6)(x + 7) + x(x + 1)(x + 2)(x + 3)(x + 5)(x + 6)(x + 7) + x(x + 1)(x + 2)(x + 4)(x + 5)(x + 6)(x + 7) + x(x + 1)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7) + x(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)*(x + 7)
5040.000000
x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)
(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 8) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 7)(x + 8) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 6)(x + 7)(x + 8) + x*(x + 1)(x + 2)(x + 3)(x + 5)(x + 6)(x + 7)(x + 8) + x*(x + 1)(x + 2)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8) + x*(x + 1)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8) + x*(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)
40320.000000
x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9)
x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8) + x(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 9) + x(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 8)(x + 9) + x(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 7)(x + 8)(x + 9) + x(x + 1)(x + 2)(x + 3)(x + 4)(x + 6)(x + 7)(x + 8)(x + 9) + x(x + 1)(x + 2)(x + 3)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9) + x(x + 1)(x + 2)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9) + x(x + 1)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9) + x(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9) + (x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)*(x + 9)
362880.000000
x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9)(x + 10)
x(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 10) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 9)(x + 10) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 8)(x + 9)(x + 10) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 7)(x + 8)(x + 9)(x + 10) + x*(x + 1)(x + 2)(x + 3)(x + 4)(x + 6)(x + 7)(x + 8)(x + 9)(x + 10) + x*(x + 1)(x + 2)(x + 3)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9)(x + 10) + x*(x + 1)(x + 2)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9)(x + 10) + x*(x + 1)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9)(x + 10) + x*(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9)(x + 10) + (x + 1)(x + 2)(x + 3)(x + 4)(x + 5)(x + 6)(x + 7)(x + 8)(x + 9)*(x + 10)
3628800.000000
分析代码结果
可以看出来
结果为
请发表评论