想用龙格库塔法计算一阶微分方程,不知道怎么在编辑框输出多行文本,求大神指导,还有程序本身异常,MFC里面的操作,新手
想用龙格库塔法计算一阶微分方程,不知道如何在编辑框输出多行文本,求大神指导,还有程序本身错误,MFC里面的操作,新手
#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
/*n表示几等分,n+1表示他输出的个数*/
int RungeKutta(double y0,double a,double b,int n,double *x,double *y,double (*function)(double,double))
{
double h=(b-a)/n,k1,k2,k3,k4;
int i;
// x=(double*)malloc((n+1)*sizeof(double));
// y=(double*)malloc((n+1)*sizeof(double));
x[0]=a;
y[0]=y0;
for(i=0;i<n;i++)
{
x[i+1]=x[i]+h;
k1=function(x[i],y[i]);
k2=function(x[i]+h/2,y[i]+h*k1/2);
k3=function(x[i]+h/2,y[i]+h*k2/2);
k4=function(x[i]+h,y[i]+h*k3);
y[i+1]=y[i]+h*(k1+2*k2+2*k3+k4)/6;
}
return 1;
}
double function(double x,double y)
{
return y-2*x/y;
}
//例子求y'=y-2*x/y(0<x<1);y0=1;
void CMyDlg::OnButton1()
{
// TODO: Add your control notification handler code here
double x[6],y[6];
int s1,s2,s3;
CString str1;
//CString str2;
char ch1[10],ch2[10],ch3[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
GetDlgItem(IDC_EDIT3)->GetWindowText(ch3,10);
s1=atoi(ch1);
s2=atoi(ch2);
s3=atoi(ch3);
RungeKutta(s1,s2,s3,5,x,y,function);
for(int i=0;i<6;i++)
{
str1.Format("%f",x[i]);
str2.Format("%f",y[i]);
GetDlgItem(IDC_EDIT4)->SetWindowText(str1);
GetDlgItem(IDC_EDIT4)->SetWindowText(str2);
}
}
------解决思路----------------------
1 编辑框设多行属性
2 文本用\r\n连接
3 龙格库塔算法在<计算方法>的附表中有,是Fortran的。翻成C++即可
#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
/*n表示几等分,n+1表示他输出的个数*/
int RungeKutta(double y0,double a,double b,int n,double *x,double *y,double (*function)(double,double))
{
double h=(b-a)/n,k1,k2,k3,k4;
int i;
// x=(double*)malloc((n+1)*sizeof(double));
// y=(double*)malloc((n+1)*sizeof(double));
x[0]=a;
y[0]=y0;
for(i=0;i<n;i++)
{
x[i+1]=x[i]+h;
k1=function(x[i],y[i]);
k2=function(x[i]+h/2,y[i]+h*k1/2);
k3=function(x[i]+h/2,y[i]+h*k2/2);
k4=function(x[i]+h,y[i]+h*k3);
y[i+1]=y[i]+h*(k1+2*k2+2*k3+k4)/6;
}
return 1;
}
double function(double x,double y)
{
return y-2*x/y;
}
//例子求y'=y-2*x/y(0<x<1);y0=1;
void CMyDlg::OnButton1()
{
// TODO: Add your control notification handler code here
double x[6],y[6];
int s1,s2,s3;
CString str1;
//CString str2;
char ch1[10],ch2[10],ch3[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
GetDlgItem(IDC_EDIT3)->GetWindowText(ch3,10);
s1=atoi(ch1);
s2=atoi(ch2);
s3=atoi(ch3);
RungeKutta(s1,s2,s3,5,x,y,function);
for(int i=0;i<6;i++)
{
str1.Format("%f",x[i]);
str2.Format("%f",y[i]);
GetDlgItem(IDC_EDIT4)->SetWindowText(str1);
GetDlgItem(IDC_EDIT4)->SetWindowText(str2);
}
}
------解决思路----------------------
1 编辑框设多行属性
2 文本用\r\n连接
3 龙格库塔算法在<计算方法>的附表中有,是Fortran的。翻成C++即可