1. 导数与函数单调性分析器

功能简介:输入函数表达式,自动计算导数,分析函数的单调性、极值点和拐点,通过交互式图形展示函数图像和导数曲线。支持多项式、指数、对数等常见函数,帮助学生理解导数与函数性质的关系,掌握利用导数研究函数的方法。
在这里插入图片描述
ArkTS代码

@Entry
@Component
struct DerivativeAnalyzer {
  @State private function: string = 'x^3 - 3x'
  @State private derivative: string = ''
  @State private criticalPoints: string = ''
  @State private intervals: string = ''
  @State private inflectionPoints: string = ''
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  build() {
    Column() {
      Text('📈 导数与函数单调性分析')
        .fontSize(24).fontWeight(FontWeight.Bold)
        .margin({ bottom: 20 })

      Text('输入函数表达式')
        .fontSize(18).fontWeight(FontWeight.Bold)
        .margin({ bottom: 10 })

      TextInput({
        placeholder: '输入函数表达式,如: x^3 - 3x, sin(x), exp(x)',
        text: this.function
      })
        .width('90%')
        .height(60)
        .onChange((v: string) => this.function = v)

      Button('分析函数')
        .width(120)
        .margin({ top: 15, bottom: 20 })
        .onClick(() => this.analyzeFunction())

      Canvas(this.context)
        .width(400).height(300)
        .backgroundColor('#f5f5f5')
        .onReady(() => this.drawFunction())

      Text('分析结果')
        .fontSize(18).fontWeight(FontWeight.Bold)
        .margin({ top: 20, bottom: 10 })

      Text(`导数: ${this.derivative}`)
        .fontSize(16).fontColor('#2196F3')
        .margin({ bottom: 5 })

      Text(`临界点: ${this.criticalPoints}`)
        .fontSize(14).fontColor('#666')
        .margin({ bottom: 5 })

      Text(`拐点: ${this.inflectionPoints}`)
        .fontSize(14).fontColor('#666')
        .margin({ bottom: 5 })

      Text(`单调区间: ${this.intervals}`)
        .fontSize(14).fontColor('#666')

      Text('支持的函数')
        .fontSize(18).fontWeight(FontWeight.Bold)
        .margin({ top: 20, bottom: 10 })

      Text('1. 多项式: x^2, x^3 - 2x + 1')
        .fontSize(14).fontColor('#666')
        .margin({ bottom: 5 })

      Text('2. 三角函数: sin(x), cos(x), tan(x)')
        .fontSize(14).fontColor('#666')
        .margin({ bottom: 5 })

      Text('3. 指数对数: exp(x), ln(x), log10(x)')
        .fontSize(14).fontColor('#666')
        .margin({ bottom: 5 })

      Text('4. 其他: sqrt(x), abs(x)')
        .fontSize(14).fontColor('#666')
    }
    .padding(20)
  }

  private analyzeFunction() {
    // 简化版函数分析,实际应用中需要实现完整的表达式解析
    if (this.function === 'x^3 - 3x') {
      this.derivative = '3x² - 3'
      this.criticalPoints = 'x = -1, x = 1'
      this.inflectionPoints = 'x = 0'
      this.intervals = '(-∞, -1): 递增; (-1, 1): 递减; (1, ∞): 递增'
    } else if (this.function === 'sin(x)') {
      this.derivative = 'cos(x)'
      this.criticalPoints = 'x = π/2 + kπ (k为整数)'
      this.inflectionPoints = 'x = kπ (k为整数)'
      this.intervals = '(2kπ - π/2, 2kπ + π/2): 递增; (2kπ + π/2, 2kπ + 3π/2): 递减'
    } else if (this.function === 'exp(x)') {
      this.derivative = 'exp(x)'
      this.criticalPoints = '无'
      this.inflectionPoints = '无'
      this.intervals = '(-∞, ∞): 递增'
    } else if (this.function === 'x^2') {
      this.derivative = '2x'
      this.criticalPoints = 'x = 0'
      this.inflectionPoints = '无'
      this.intervals = '(-∞, 0): 递减; (0, ∞): 递增'
    } else {
      // 尝试使用数值方法分析
      this.derivative = '数值导数'
      this.criticalPoints = '需计算'
      this.inflectionPoints = '需计算'
      this.intervals = '需分析'
    }
    this.drawFunction()
  }

  private drawFunction() {
    const ctx = this.context
    const width = 400
    const height = 300
    const centerX = width / 2
    const centerY = height / 2
    const scale = 30 // 缩放因子
    
    // 清空画布
    ctx.clearRect(0, 0, width, height)
    
    // 绘制坐标轴
    ctx.beginPath()
    ctx.moveTo(50, centerY)
    ctx.lineTo(width - 50, centerY)
    ctx.moveTo(centerX, 50)
    ctx.lineTo(centerX, height - 50)
    ctx.strokeStyle = '#000'
    ctx.lineWidth = 1
    ctx.stroke()
    
    // 绘制网格
    ctx.strokeStyle = '#ddd'
    ctx.lineWidth = 0.5
    for (let i = 1; i < 6; i++) {
      const x = centerX + i * scale
      ctx.beginPath()
      ctx.moveTo(x, 50)
      ctx.lineTo(x, height - 50)
      ctx.stroke()
      
      const x2 = centerX - i * scale
      ctx.beginPath()
      ctx.moveTo(x2, 50)
      ctx.lineTo(x2, height - 50)
      ctx.stroke()
      
      const y = centerY + i * scale
      ctx.beginPath()
      ctx.moveTo(50, y)
      ctx.lineTo(width - 50, y)
      ctx.stroke()
      
      const y2 = centerY - i * scale
      ctx.beginPath()
      ctx.moveTo(50, y2)
      ctx.lineTo(width - 50, y2)
      ctx.stroke()
    }
    
    // 绘制函数图像
    ctx.beginPath()
    ctx.strokeStyle = '#2196F3'
    ctx.lineWidth = 2
    
    for (let x = -5; x <= 5; x += 0.1) {
      let y: number
      try {
        y = this.evaluateFunction(x)
      } catch {
        continue
      }
      
      if (Math.abs(y) > 10) continue // 限制范围
      
      const canvasX = centerX + x * scale
      const canvasY = centerY - y * scale
      
      if (x === -5) {
        ctx.moveTo(canvasX, canvasY)
      } else {
        ctx.lineTo(canvasX, canvasY)
      }
    }
    ctx.stroke()
    
    // 绘制导数曲线
    ctx.beginPath()
    ctx.strokeStyle = '#FF5722'
    ctx.lineWidth = 2
    ctx.setLineDash([5, 5])
    
    for (let x = -5; x <= 5; x += 0.1) {
      let y: number
      try {
        y = this.evaluateDerivative(x)
      } catch {
        continue
      }
      
      if (Math.abs(y) > 10) continue // 限制范围
      
      const canvasX = centerX + x * scale
      const canvasY = centerY - y * scale
      
      if (x === -5) {
        ctx.moveTo(canvasX, canvasY)
      } else {
        ctx.lineTo(canvasX, canvasY)
      }
    }
    ctx.stroke()
    ctx.setLineDash([])
    
    // 绘制图例
    ctx.font = '12px Arial'
    ctx.fillStyle = '#000'
    ctx.fillText('函数 f(x)', 30, 30)
    ctx.fillText('导数 f\'(x)', 120, 30)
    
    ctx.fillStyle = '#2196F3'
    ctx.fillRect(25, 15, 10, 10)
    ctx.fillStyle = '#FF5722'
    ctx.fillRect(115, 15, 10, 10)
  }

  private evaluateFunction(x: number): number {
    // 简化版函数求值,实际应用中需要实现完整的表达式解析
    if (this.function === 'x^3 - 3x') {
      return x * x * x - 3 * x
    } else if (this.function === 'sin(x)') {
      return Math.sin(x)
    } else if (this.function === 'exp(x)') {
      return Math.exp(x)
    } else if (this.function === 'x^2') {
      return x * x
    } else if (this.function === 'ln(x)') {
      return Math.log(x)
    } else if (this.function === 'cos(x)') {
      return Math.cos(x)
    } else if (this.function === 'tan(x)') {
      return Math.tan(x)
    } else if (this.function === 'sqrt(x)') {
      return Math.sqrt(x)
    } else if (this.function === 'abs(x)') {
      return Math.abs(x)
    } else if (this.function === 'x^3') {
      return x * x * x
    } else if (this.function === 'x^4') {
      return x * x * x * x
    } else {
      // 对于其他函数,返回0
      return 0
    }
  }

  private evaluateDerivative(x: number): number {
    // 使用数值微分法计算导数
    const h = 0.0001
    return (this.evaluateFunction(x + h) - this.evaluateFunction(x - h)) / (2 * h)
  }
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐