利用java代码求定积分,已知值和下限求上限,有没有会的大神呀
晚点,我装一个java 环境,试着写一写
import static java.lang.Math.abs;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
public class Integral extends JFrame{
private JLabel rooth1=new JLabel("系数:");
private JLabel rooth2=new JLabel("指数:");
private JLabel rooth3=new JLabel("结果:");
private JLabel rooth4=new JLabel("积分上限:");
private JLabel rooth5=new JLabel("积分下限:");
private JTextField tf1=new JTextField();
private JTextField tf2=new JTextField();
private JTextField tf3=new JTextField();
private JTextField tf4=new JTextField();
private JTextField tf5=new JTextField();
private JButton bt1=new JButton("点击求解");
ArrayList<Double> coef=new ArrayList<Double>();//系数
ArrayList<Double> index=new ArrayList<Double>();//指数
double x0,xn;//积分上限,下限
public Integral()
{
this.setContentPane(new MyPanel());
setTitle("JAVA:求函数积分");
setLayout(null);
setSize(700,500);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); // 获得屏幕的大小
Dimension frm = this.getSize();
setLocation((scr.width - frm.width) / 2,
(scr.height - frm.height) / 2 - 18);
rooth1.setFont(new Font("楷书", Font.BOLD, 20));
rooth1.setForeground(new Color(240, 120, 100));
rooth2.setFont(new Font("楷书", Font.BOLD, 20));
rooth2.setForeground(new Color(240, 120, 100));
rooth3.setFont(new Font("楷书", Font.BOLD, 20));
rooth3.setForeground(new Color(240, 120, 100));
rooth4.setFont(new Font("楷书", Font.BOLD, 20));
rooth4.setForeground(new Color(240, 120, 100));
rooth5.setFont(new Font("楷书", Font.BOLD, 20));
rooth5.setForeground(new Color(240, 120, 100));
rooth1.setBounds(100,140,150,50);
rooth2.setBounds(100,200,150,30);
rooth3.setBounds(100,350,150,30);
rooth4.setBounds(100,50,150,30);
rooth5.setBounds(100,100,150,30);
tf1.setBounds(220,150,220,30);
tf2.setBounds(220,200,220,30);
tf3.setBounds(220,350,220,50);
tf4.setBounds(220,50,220,30);
tf5.setBounds(220,100,220,30);
bt1.setBounds(270,280,90,30);
add(rooth1);
add(rooth2);
add(rooth3);
add(rooth4);
add(rooth5);
add(tf1);
add(tf2);
add(tf3);
add(tf4);
add(tf5);
add(bt1);
setVisible(true);
tf1.addActionListener( //系数
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double temp1;
temp1=Double.parseDouble(tf1.getText());
coef.add(temp1);
}
});
tf2.addActionListener( //指数
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double temp2;
temp2=Double.parseDouble(tf2.getText());
index.add(temp2);
}
});
tf4.addActionListener( //积分上限
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
x0=Double.parseDouble(tf4.getText());
}
});
tf5.addActionListener( //积分下限
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
xn=Double.parseDouble(tf5.getText());
}
});
bt1.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ double result1=0;
for(int i=0;i<coef.size();i++)
{ System.out.println("coef="+coef.get(i));
System.out.println("index="+index.get(i));
}
System.out.println("x0="+x0+" xn="+xn);
result1=getresult(x0,xn);
tf3.setText(result1+" ");
}
});
}
public double f(double xi) //定义f(x)函数,
{
double f = 0;
for(int i=0;i<coef.size();i++)
{
f+=coef.get(i)*Math.pow(xi,index.get(i));
}
return f;
}
public double getresult(double x0,double xn)//用矩形法求定积分(左边界)
{
double result=0;
int n=1000;//将x区间分为n份
double h = abs(xn - x0) / n; //小矩形的宽度
for(double xi=x0;xi<xn;xi=xi+h)
{
result+=f(xi)*h;
}
return result;
}
public static void main(String args[])
{
Integral integral=new Integral();
}
private class MyPanel extends JPanel
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage(
"./src/images/java.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}
看哈这个能不能解决你的问题
求R的值
你好,我是问答小助手,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。