⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nonlinearequationdlg.cpp

📁 清华大学《数值分析A》-第07章.非线性方程求根 二分法 迭代法 Steffensen加速 Newton法 弦截法 抛物线法
💻 CPP
字号:
// NonlinearEquationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NonlinearEquation.h"
#include "NonlinearEquationDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNonlinearEquationDlg dialog

CNonlinearEquationDlg::CNonlinearEquationDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNonlinearEquationDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNonlinearEquationDlg)
	m_dblMethod1 = 0.0;
	m_dblMethod2 = 0.0;
	m_dblSteffensen1 = 0.0;
	m_dblSteffensen2 = 0.0;
	m_dblNewton = 0.0;
	m_nMethod1 = 0;
	m_nMethod2 = 0;
	m_nSteffensen1 = 0;
	m_nSteffensen2 = 0;
	m_nNewton = 0;
	m_dblEpsilon = 1e-8;
	m_dblX0 = 1.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_nMax			=	int(1e6);
	X				=	new double[m_nMax];
}

void CNonlinearEquationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNonlinearEquationDlg)
	DDX_Text(pDX, IDC_EDIT_METHOD1, m_dblMethod1);
	DDX_Text(pDX, IDC_EDIT_METHOD2, m_dblMethod2);
	DDX_Text(pDX, IDC_EDIT_STEFFENSEN1, m_dblSteffensen1);
	DDX_Text(pDX, IDC_EDIT_STEFFENSEN2, m_dblSteffensen2);
	DDX_Text(pDX, IDC_EDIT_NEWTON, m_dblNewton);
	DDX_Text(pDX, IDC_EDIT_N1, m_nMethod1);
	DDX_Text(pDX, IDC_EDIT_N2, m_nMethod2);
	DDX_Text(pDX, IDC_EDIT_N3, m_nSteffensen1);
	DDX_Text(pDX, IDC_EDIT_N4, m_nSteffensen2);
	DDX_Text(pDX, IDC_EDIT_N5, m_nNewton);
	DDX_Text(pDX, IDC_EDIT_EPSILON, m_dblEpsilon);
	DDX_Text(pDX, IDC_EDIT_X0, m_dblX0);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNonlinearEquationDlg, CDialog)
	//{{AFX_MSG_MAP(CNonlinearEquationDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_METHOD1, OnButtonMethod1)
	ON_BN_CLICKED(IDC_BUTTON_METHOD2, OnButtonMethod2)
	ON_BN_CLICKED(IDC_BUTTON_STEFFENSEN1, OnButtonSteffensen1)
	ON_BN_CLICKED(IDC_BUTTON_STEFFENSEN2, OnButtonSteffensen2)
	ON_BN_CLICKED(IDC_BUTTON_NEWTON, OnButtonNewton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNonlinearEquationDlg message handlers

BOOL CNonlinearEquationDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNonlinearEquationDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNonlinearEquationDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNonlinearEquationDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CNonlinearEquationDlg::OnButtonMethod1() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	X[0] = m_dblX0;

	for(int i=1; i<m_nMax; i++)
	{
		X[i] = FuncMethod1(X[i-1]);
		if(fabs(X[i]-X[i-1]) < m_dblEpsilon)
		{
			m_dblMethod1 = X[i];
			m_nMethod1 = i;
			break;
		}
	}

	if(i == m_nMax)
	{
		MessageBox(_T("发散"));
	}
	else
	{
		CStdioFile pMethod1;
		CString strFileMethod1;
		strFileMethod1.Format(_T("ResultMethod1迭代%d次.txt"), m_nMethod1);
		pMethod1.Open(strFileMethod1, CFile::modeCreate | CFile::modeWrite);

		CString str		=	_T("");

		for(int i=0; i<=m_nMethod1; i++)
		{
			str.Format(_T("n=%d	x=%15.12f\n"), i, X[i]);
			pMethod1.WriteString(str);
		}
		pMethod1.Close();
	}

	UpdateData(FALSE);
}

void CNonlinearEquationDlg::OnButtonMethod2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	X[0] = m_dblX0;

	for(int i=1; i<m_nMax; i++)
	{
		X[i] = FuncMethod2(X[i-1]);
		if(fabs(X[i]-X[i-1]) < m_dblEpsilon)
		{
			m_dblMethod2 = X[i];
			m_nMethod2 = i;
			break;
		}
	}

	if(i == m_nMax)
	{
		MessageBox(_T("发散"));
	}
	else
	{
		CStdioFile pMethod2;
		CString strFileMethod2;
		strFileMethod2.Format(_T("ResultMethod2迭代%d次.txt"), m_nMethod2);
		pMethod2.Open(strFileMethod2, CFile::modeCreate | CFile::modeWrite);

		CString str		=	_T("");

		for(int i=0; i<=m_nMethod2; i++)
		{
			str.Format(_T("n=%d	x=%15.12f\n"), i, X[i]);
			pMethod2.WriteString(str);
		}
		pMethod2.Close();
	}

	UpdateData(FALSE);
}

void CNonlinearEquationDlg::OnButtonSteffensen1() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	X[0] = m_dblX0;

	for(int i=1; i<m_nMax; i++)
	{
		X[i] = X[i-1] - (FuncMethod1(X[i-1])-X[i-1])*(FuncMethod1(X[i-1])-X[i-1])/(FuncMethod1(FuncMethod1(X[i-1]))-2*FuncMethod1(X[i-1])+X[i-1]);
		if(fabs(X[i]-X[i-1]) < m_dblEpsilon)
		{
			m_dblSteffensen1 = X[i];
			m_nSteffensen1 = i;
			break;
		}
	}

	if(i == m_nMax)
	{
		MessageBox(_T("发散"));
	}
	else
	{
		CStdioFile pSteffensen1;
		CString strFileSteffensen1;
		strFileSteffensen1.Format(_T("ResultSteffensen1迭代%d次.txt"), m_nSteffensen1);
		pSteffensen1.Open(strFileSteffensen1, CFile::modeCreate | CFile::modeWrite);

		CString str		=	_T("");

		for(int i=0; i<=m_nSteffensen1; i++)
		{
			str.Format(_T("n=%d	x=%15.12f\n"), i, X[i]);
			pSteffensen1.WriteString(str);
		}
		pSteffensen1.Close();
	}

	UpdateData(FALSE);
}

void CNonlinearEquationDlg::OnButtonSteffensen2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	X[0] = m_dblX0;

	for(int i=1; i<m_nMax; i++)
	{
		X[i] = X[i-1] - (FuncMethod2(X[i-1])-X[i-1])*(FuncMethod2(X[i-1])-X[i-1])/(FuncMethod2(FuncMethod2(X[i-1]))-2*FuncMethod2(X[i-1])+X[i-1]);
		if(fabs(X[i]-X[i-1]) < m_dblEpsilon)
		{
			m_dblSteffensen2 = X[i];
			m_nSteffensen2 = i;
			break;
		}
	}

	if(i == m_nMax)
	{
		MessageBox(_T("发散"));
	}
	else
	{
		CStdioFile pSteffensen2;
		CString strFileSteffensen2;
		strFileSteffensen2.Format(_T("ResultSteffensen2迭代%d次.txt"), m_nSteffensen2);
		pSteffensen2.Open(strFileSteffensen2, CFile::modeCreate | CFile::modeWrite);

		CString str		=	_T("");

		for(int i=0; i<=m_nSteffensen2; i++)
		{
			str.Format(_T("n=%d	x=%15.12f\n"), i, X[i]);
			pSteffensen2.WriteString(str);
		}
		pSteffensen2.Close();
	}

	UpdateData(FALSE);
}

void CNonlinearEquationDlg::OnButtonNewton() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	X[0] = m_dblX0;

	for(int i=1; i<m_nMax; i++)
	{
		X[i] = FuncNewton(X[i-1]);
		if(fabs(X[i]-X[i-1]) < m_dblEpsilon)
		{
			m_dblNewton = X[i];
			m_nNewton = i;
			break;
		}
	}

	if(i == m_nMax)
	{
		MessageBox(_T("发散"));
	}
	else
	{
		CStdioFile pNewton;
		CString strFileNewton;
		strFileNewton.Format(_T("ResultNewton迭代%d次.txt"), m_nNewton);
		pNewton.Open(strFileNewton, CFile::modeCreate | CFile::modeWrite);

		CString str		=	_T("");

		for(int i=0; i<=m_nNewton; i++)
		{
			str.Format(_T("n=%d	x=%15.12f\n"), i, X[i]);
			pNewton.WriteString(str);
		}
		pNewton.Close();
	}

	UpdateData(FALSE);
}

double CNonlinearEquationDlg::FuncMethod1(double x)
{
	return 20/(x*x+2*x+10);
}

double CNonlinearEquationDlg::FuncMethod2(double x)
{
	return (20-2*x*x-x*x*x)/10;
}

double CNonlinearEquationDlg::FuncNewton(double x)
{
	return x - (x*x*x+2*x*x+10*x-20)/(2*x*x+4*x+10);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -