You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C语言打印指定图像?以及如何用fprint函数基于给定方程绘图?

How to Plot the Curve x(t) = -2tsin(t²) in C (Console Output + fprintf Data Export)

Got it, let's walk through two practical ways to work with this curve in C: first, printing a text-based plot directly to your console, and second, using fprintf to save data points for polished plotting with external tools like Gnuplot or Excel.

1. Console ASCII Plot

This method draws a simple, readable plot right in your terminal. The core idea is to map calculated x(t) values to columns in your console, then print a marker (like *) at the correct position for each t value.

Here's the code with clear explanations:

#define _USE_MATH_DEFINES  // Required for M_PI on some compilers
#include <stdio.h>
#include <math.h>
#include <float.h>  // For INFINITY constant

int main() {
    // Configure t range and step size (tweak these to adjust the plot)
    double t_start = 0.0;
    double t_end = 4 * M_PI;  // Covers multiple oscillations of the curve
    double t_step = 0.1;      // Smaller step = smoother plot, more lines

    // Set console width (columns) for the plot
    const int console_cols = 80;

    // First, calculate min/max x(t) values to avoid clipping the plot
    double x_min = INFINITY, x_max = -INFINITY;
    for (double t = t_start; t <= t_end; t += t_step) {
        double x = -2 * t * sin(t * t);
        if (x < x_min) x_min = x;
        if (x > x_max) x_max = x;
    }
    double x_range = x_max - x_min;

    // Print plot header
    printf("ASCII Plot of x(t) = -2*t*sin(t^2) (t from 0 to 4π):\n");
    
    // Draw each point of the curve
    for (double t = t_start; t <= t_end; t += t_step) {
        double x = -2 * t * sin(t * t);
        // Map x value to a console column (0 to console_cols-1)
        int col = (int)(((x - x_min) / x_range) * (console_cols - 1));
        // Print spaces up to the column, then a marker
        for (int i = 0; i < col; i++) {
            printf(" ");
        }
        printf("*\n");
    }

    return 0;
}

Compilation Tip:

When compiling, link the math library with the -lm flag:

gcc plot_console.c -o plot_console -lm

2. Using fprintf to Export Data for External Plotting

For a precise, high-quality plot, use fprintf to write t and x(t) values to a text file. You can then import this file into tools like Gnuplot, Excel, or Python's Matplotlib to generate a professional graph.

Here's the implementation:

#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>

int main() {
    // Open a file to store data (creates/overwrites curve_data.txt)
    FILE *data_file = fopen("curve_data.txt", "w");
    if (data_file == NULL) {
        perror("Error opening file");
        return 1;
    }

    // Write a header line (optional but makes the file easier to read)
    fprintf(data_file, "t_value\tx(t)\n");

    // Configure t range and step (smaller step = smoother curve)
    double t_start = 0.0;
    double t_end = 4 * M_PI;
    double t_step = 0.01;

    // Calculate and write each data point
    for (double t = t_start; t <= t_end; t += t_step) {
        double x = -2 * t * sin(t * t);
        // Write values with 4 decimal places for readability
        fprintf(data_file, "%.4f\t%.4f\n", t, x);
    }

    // Always close the file after writing!
    fclose(data_file);
    printf("Data saved to curve_data.txt. Use Gnuplot/Excel to plot!\n");

    return 0;
}

How to Plot the Exported Data:

  • Gnuplot: Open Gnuplot and run:
    plot 'curve_data.txt' using 1:2 with lines title "x(t) = -2*t*sin(t^2)"
    
  • Excel: Import the text file (use Tab as the delimiter), then create a line chart with t values on the x-axis and x(t) on the y-axis.

Quick Notes:

  • Adjust t_start, t_end, and t_step to focus on specific sections of the curve.
  • The _USE_MATH_DEFINES macro is needed for M_PI on compilers like MSVC; GCC usually works without it, but adding it won't cause issues.

内容的提问来源于stack exchange,提问作者jsjzyzc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:03:56