實驗三 數值積分(android)

來源:互聯網
上載者:User

標籤:

實驗二部落格地址:http://blog.csdn.net/double2hao/article/details/51217356

實驗一部落格地址:http://blog.csdn.net/double2hao/article/details/51152843


一、實驗內容

分別寫出變步長梯形法和romberge法計算定積分的演算法,編寫程式上機調試出結果,要求所編程式適用於任何類型的定積分,即能解決這一類問題,而不是某一個問題。

實驗中以下列資料驗證程式的正確性。

求  (sinx)/x的積分,積分區間為[0,1]


效果:(源碼在文章底部)

  


主要工作:

1、添加了ThreeFragment的xml介面

2、掌握理解變步長梯形法和龍貝格法


主要邏輯代碼:

ThreeFragment:

package com.example.double2.numericcalculationtest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.Spinner;import android.widget.TextView;/** * 項目名稱:NumericCalculationTest * 建立人:Double2號 * 建立時間:2016/4/13 21:41 * 修改備忘: */public class ThreeFragment extends Fragment {    private View views;    private Spinner mSpinner;    private final String[] spinnerChose = {"變步長梯形法", "龍貝格法"};    private Button btnSure;    private TextView tvResult;    private String stringResult;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        views = inflater.inflate(R.layout.fra_three, null);        initView();        return views;    }    private void initView() {        mSpinner = (Spinner) views.findViewById(R.id.sp_three_chose);        btnSure = (Button) views.findViewById(R.id.btn_three_sure);        tvResult = (TextView) views.findViewById(R.id.tv_three_result);        mSpinner.setAdapter(new ArrayAdapter<String>(getActivity(),                android.R.layout.simple_list_item_1, spinnerChose));        btnSure.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                caculating();            }        });    }    private void caculating() {        if (mSpinner.getSelectedItemPosition() == 0) {            TrapezoidCaculation();        } else {            RombergCaculation();        }    }    //變步長梯形法求值    private void TrapezoidCaculation() {        Double a = 0.0, b = 1.0, h;        int m = 1, n = 0;        Double[] T = new Double[100];//固定只能計算100次        T[n] = (b - a) * (f(a) + f(b)) / 2;        do {            h = (b - a) / m;            double s = 0.0;            for (int k = 0; k < m; k++) {                s += f(a + (k + 0.5) * h);            }            T[n + 1] = T[n] / 2 + h / 2 * s;            m = 2 * m;            n++;        } while (Math.abs(T[n] - T[n - 1]) >= 0.0000001);        stringResult = "n= " + n +                "\nS= " + T[n]                + "\n誤差為 " + Math.abs(T[n] - T[n - 1]);        tvResult.setText(stringResult);    }    //龍貝格法求值    private void RombergCaculation() {        Double a = 0.0, b = 1.0, h;        Double[][] T = new Double[100][100];//固定只能計算100次        int i = 0;        T[i][i] = (b - a) * (f(a) + f(b)) / 2;        do {            i++;            double s = 0;            for (int j = 0; j <= Math.pow(2, i - 1) - 1; j++) {                s += f(a + (2 * j + 1) * (b - a) / Math.pow(2, i));            }            s = s * (b - a) / Math.pow(2, i);            s += T[i - 1][0] / 2;            T[i][0] = s;            for (int m = 1; m <= i; m++) {                T[i][m] = (Math.pow(4, m) * T[i][m - 1] - T[i - 1][m - 1]) / (Math.pow(4, m) - 1);            }        } while (Math.abs(T[i][i] - T[i - 1][i - 1]) >= 0.0000001);        stringResult = "n= " + i +                "\nS= " + T[i][i]                + "\n誤差為 " + Math.abs(T[i][i] - T[i - 1][i - 1]);        tvResult.setText(stringResult);    }    double f(double x) {        double y;        //如果x等於0,就直接返回1        if (x == 0)            y = 1;        else y = Math.sin(x) / x;        return (y);    }}

源碼地址:http://download.csdn.net/detail/double2hao/9513415

實驗三 數值積分(android)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.