欧拉法求解y'=-2y-4x, x0=0, y0=2, x<=1的求解如下:
#include<stdio.h>
/*solve ode: dy/dx = -2*y -4*x*/
float fun(float x,float y){
float f
f=-2.0*y -4.0*x
return f
}
int main(){
float x0=0,y0=2.0,x,y,h=0.1,t=1.0,k
/* printf("\nEnter x0,y0,h,xn: ") scanf("%f%f%f%f",&x0,&y0,&h,&t)*/
x=x0
y=y0
printf("\n x\t y\n")
while(x<=t){
k=h*fun(x,y)
y=y+k
x=x+h
printf("%0.3f\t%0.3f\n",x,y)
}return 0
}
代码截图
运行结果
代码截图+运行结果
(晚点我再来看后面的几小问)
f=inline('x*y','x','y')%微分方程的右边项 dx=0.05%x方向步长 xleft=0%区域的左边界 xright=3%区域的右边界 xx=xleft:dx:xright%一系列离散的点 n=length(xx)%点的个数 y0=1%%(1)欧拉法 Euler=y0fori=2:n Euler(i)=...// zifuchuan.cpp : Defines the entry point for the console application.//
#include "stdio.h"
#include “stdlib.h”
#define N 20
//#define exit 0
int length(char *p)
{
int i,count=0
for(i=0p[i]!='\0'i++)
count++
return count
}
void copy(char *p1,char *p2)
{
int i
for(i=0p2[i]!='\0'i++)
p1[i]=p2[i]
if(p1[i]!='\0')
p1[i]='\0'
printf("复制完成\n")
printf("%s\n",p1)
}
int compare(char *p1,char *p2)
{
int i,j
for(i=0p1[i]!='\0'||p2[i]!='\0'i++)
if(p1[i]!=p2[i])
{
j=p1[i]-p2[i]
return j
}
return 0
}
int main(int argc, char* argv[])
{
char p1[20],p2[20]
int e,f
printf("请输入字符串p1\n")
scanf("%s",p1)
printf("请输入字符串p2\n")
scanf("%s",p2)
// printf("请输入字符串p2\n")
// scanf("%s",p2)
while(1)
{
printf("----------1.求字符串长度----------\n")
printf("------------2.复制拷贝字符串----------\n")
printf("------------3.比较字符串------------\n")
printf("--------------4.退出程序--------------\n")
int choose
printf("请选择:")
scanf("%d",&choose)
switch(choose)
{
case 1:e=length(p1)printf("%d\n",e)break
case 2:copy(p1,p2)break
case 3:f=compare(p1,p2)printf("%d\n",f)break
case 4:exit(0)
}
}
}