實驗三 數值積分(android)

許佳佳233發表於2016-05-08

實驗二部落格地址: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

相關文章