一、代码实现
微分(实际上是导数,微分不用除dx):
python代码:
x=0
dx=0.00001
def f(x):
y=2*x
return y
print((f(x+dx)-f(x))/dx)
C语言代码:
#include<stdio.h>
double x = 1;
double dx = 0.00001;
double f(double arg) {
double y = 2 * arg;
return y;
}
int main() {
printf("%f", (f(x + dx) - f(x)) / dx);
return 0;
}
C++代码:
#include <iostream>
double x = 1;
double dx = 0.00001;
double f(double arg) {
double y = 2 * arg;
return y;
}
int main() {
std::cout << (f(x + dx) - f(x)) / dx << std::endl;
return 0;
}
积分(定积分):
python:
dx=0.00001
a=0
b=1
i=a
s=0
def f(x):
y=x
return y
while i<=b:
s+=f(i)
i+=dx
print(s*dx)
C:
#include <stdio.h>
double dx = 0.00001;
double a = 0;
double b = 1;
double i = a;
double s = 0;
double f(double x) {
double y = x;
return y;
}
int main() {
while (i <= b) {
s += f(i);
i += dx;
}
printf("%f\n", s * dx);
return 0;
}
C++:
#include <iostream>
double dx = 0.00001;
double a = 0;
double b = 1;
double i = a;
double s = 0;
double f(double x) {
double y = x;
return y;
}
int main() {
while (i <= b) {
s += f(i);
i += dx;
}
std::cout << s * dx << std::endl;
return 0;
}
二、解释
很好理解,导数中,x是哪一点的导数,dx是精准度,y等于f(x)即函数。导数结果就是瞬时变化率
F(x)为原函数,即微分后的函数是f(x)dx,微分前是F(x)
积分很好理解,a为下限,b为上限,y为函数,结果就是y=f(x)下a到b的面积
才几十行代码,微积分也不是很难啊
三、应用
解一个变速行程问题
一列车初始速度为10m/s,加速度为,问速度达到20时车走了多少米
只要用定积分代入a=0,b=10(10s)y=x(1*x=1)就得到结果
= 50.00004999944839
标签:return,python,double,0.00001,C++,int,dx,arg,高数 From: https://blog.csdn.net/linhhanpy/article/details/141245647