一个函数x(t)=-2tsin(tt),0≤t≤8,从0开始,以△t=0.01、0.02,0.04、0.08、0.1,0.25,0.5、1等为间隔,分别绘制不同△t下的x(t)的离散曲线。每个离散样本点可用‘’显示,程序采用JAVA实现。
专门配置环境帮你弄了,记得采纳一下哦!谢谢啦!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class DiscretizeFunctionCurve extends Application {
@Override
public void start(Stage stage) {
stage.setTitle("Discrete Curve");
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("t");
yAxis.setLabel("x(t)");
final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Discrete Curve: x(t)=-2tsin(t^3)");
double[] intervals = {0.01, 0.02, 0.04, 0.08, 0.1, 0.25, 0.5, 1};
for (double interval : intervals) {
XYChart.Series series = new XYChart.Series();
series.setName("Interval: " + interval);
for (double t = 0; t < 8; t += interval) {
double x = -2 * t * Math.sin(Math.pow(t, 3));
series.getData().add(new XYChart.Data(t, x));
}
lineChart.getData().add(series);
}
Scene scene = new Scene(lineChart, 800, 600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
以下回答参考GPT,并由JoseKe整理完成,希望您能采纳:java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class DiscreteCurve extends JPanel {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
JFrame frame = new JFrame("离散函数曲线");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DiscreteCurve());
frame.pack();
frame.setSize(1000,600);
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
double x_min = 0, x_max = 8; // 定义函数范围
double f_min = Double.MAX_VALUE, f_max = Double.MIN_VALUE;
double[] x = new double[(int)((x_max - x_min) / 0.01) + 1];
double[] f = new double[(int)((x_max - x_min) / 0.01) + 1];
double t = 0;
int i = 0;
while (t <= x_max) {
x[i] = t;
f[i] = -2 * t * Math.sin(t * t);
if (f[i] < f_min) f_min = f[i];
if (f[i] > f_max) f_max = f[i];
t += 0.01;
i++;
}
double x_scale = getWidth() / (x_max - x_min); // x轴缩放比例
double y_scale = getHeight() / (f_max - f_min); // y轴缩放比例
double x_offset = -x_min * x_scale; // x轴偏移量
double y_offset = f_max * y_scale; // y轴偏移量
Path2D.Double path = new Path2D.Double();
path.moveTo(x[0] * x_scale + x_offset, y_offset - f[0] * y_scale);
for (int j = 1; j < x.length; j++) {
path.lineTo(x[j] * x_scale + x_offset, y_offset - f[j] * y_scale);
}
g2.draw(path);
}
}
引用chatGPT作答,以下是JAVA实现的代码,实现对给定函数 x(t) 的离散曲线绘制:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DiscreteCurve extends JPanel {
private double[] x;
private double[] t;
private int n;
public DiscreteCurve(double[] x, double[] t) {
this.x = x;
this.t = t;
this.n = x.length;
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.add(this);
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(50, 500, 750, 500);
g.drawLine(50, 500, 50, 50);
g.drawString("t", 760, 520);
g.drawString("x", 40, 40);
g.drawString("0", 45, 515);
g.drawString("8", 745, 515);
g.drawString("-16", 20, 505);
g.drawString("16", 20, 80);
for (int i = 0; i < n; i++) {
int xCoord = (int) ((t[i] / 8) * 700) + 50;
int yCoord = 500 - (int) ((x[i] / 16) * 450);
g.drawString("●", xCoord, yCoord);
}
}
public static void main(String[] args) {
double[] t = new double[801];
double[] x = new double[801];
for (int i = 0; i < 801; i++) {
t[i] = i * 0.01;
x[i] = -2 * t[i] * Math.sin(t[i] * t[i]);
}
DiscreteCurve dc = new DiscreteCurve(x, t);
}
}
上面的代码使用了JFrame和JPanel来绘制离散曲线。在构造函数中,我们将离散点的横坐标t和纵坐标x传递给了类的成员变量。在paintComponent方法中,我们先绘制了坐标轴和标注,并通过循环将每个离散点绘制为圆点。最后在main方法中,我们调用了DiscreteCurve类来生成离散曲线图。
注意:在代码中,我们假设了函数在t=0和t=8时有801个离散点。这是因为我们取的最小时间间隔△t是0.01,因此在区间[0,8]中有800个时间点,再加上t=0和t=8这两个端点,总共有801个时间点。如果需要绘制其他时间间隔下的离散曲线,只需要根据时间间隔△t的不同,修改循环中的时间点数量即可。
可以借鉴下
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int lsh[1000], t[1000], sy[1000];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&sy[i]);
t[i]=sy[i];
}
sort(sy+1,sy+n+1);
int size=unique(sy,sy+n)-sy;
printf("size is : %d",size);
printf("\n");
for(int i=1;i<=n;i++)
{
lsh[i]=lower_bound(sy,sy+size,t[i])-sy;
}
}