實驗四 常微分方程的數值解(android)

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

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

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

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


【實驗內容】

分別寫出改進尤拉法四階龍格庫塔求解的演算法,編寫成熟上機除錯出結果,要求所程式設計序適用於任何一階常微分方程的數值解問題,即能解決這一類問題,而不是某一個問題。

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

y'=-xy^2

y(0)=2

(0<=x<=5)  步長h=0.25


效果:(原始碼在文章結尾)

  



主要工作:

1、新增了FourFragment的xml介面


2、掌握理解改進尤拉法與四階龍格庫塔法



主要邏輯程式碼

FourFragment:

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;

import java.text.DecimalFormat;

/**
 * 專案名稱:NumericCalculationTest
 * 建立人:Double2號
 * 建立時間:2016/4/13 21:41
 * 修改備註:
 */
public class FourFragment extends Fragment {

    private View views;
    private Spinner mSpinner;
    private final String[] spinnerChose = {"改進尤拉法", "四階龍格庫塔法"};
    private Button btnSure;
    private TextView tvResult;
    private String stringResult;
    final double h=0.25;
    private DecimalFormat mDecimalFormat = new DecimalFormat("0.00");//保留兩位小數

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        views = inflater.inflate(R.layout.fra_four, null);
        initView();
        return views;
    }

    private void initView() {
        mSpinner = (Spinner) views.findViewById(R.id.sp_four_chose);
        btnSure = (Button) views.findViewById(R.id.btn_four_sure);
        tvResult = (TextView) views.findViewById(R.id.tv_four_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) {
            EulerCaculation();
        } else {
            RungeKuttaCaculation();
        }
    }

    private void EulerCaculation() {
        double[] x=new double[21];
        double[] y=new double[21];
        double[] y_next=new double[21];

        x[0]=0;
        for(int i=1;i<21;i++) {
            x[i]=x[i-1]+h;
        }
        y[0]=2;
        stringResult="x= "+mDecimalFormat.format(x[0])+" y= "+y[0]+"\n";

        for(int i=0;i<20;i++){
            y_next[i]=y[i]+h*f(x[i],y[i]);
            y[i+1]=y[i]+(f(x[i],y[i])+f(x[i+1],y_next[i]))*h/2;
            stringResult+="x= "+mDecimalFormat.format(x[i+1])+" y= "+y[i+1]+"\n";
        }
        tvResult.setText(stringResult);
    }

    private void RungeKuttaCaculation() {
        double[] x=new double[21];
        double[] y=new double[21];
        double[] k=new double[4];

        x[0]=0;
        for(int i=1;i<21;i++) {
            x[i]=x[i-1]+h;
        }
        y[0]=2;
        stringResult="x= "+mDecimalFormat.format(x[0])+" y= "+y[0]+"\n";

        for(int i=0;i<20;i++){
            k[0]=f(x[i],y[i]);
            k[1]=f(x[i]+h/2,y[i]+k[0]*h/2);
            k[2]=f(x[i]+h/2,y[i]+k[1]*h/2);
            k[3]=f(x[i]+h,y[i]+h*k[2]);
            y[i+1]=y[i]+(k[0]+2*k[1]+2*k[2]+k[3])*h/6;
            stringResult+="x= "+mDecimalFormat.format(x[i+1])+" y= "+y[i+1]+"\n";
        }
        tvResult.setText(stringResult);

    }

    double f(double x,double y) {
        return -(x*Math.pow(y,2));
    }
}


原始碼地址:http://download.csdn.net/detail/double2hao/9516426

相關文章