matlab如何用欧拉法求解非线性微分方程组？

``````function [t,x1,x2]=Euler(x10,x20,a,b,c,d,h,T)
%x10,x20为初值条件，h为步长，T为取值区间
n=round((T(2)-T(1))/h)+1;           %计算离散点的个数
t=zeros(n,1);
x1=zeros(n,1);
x2=zeros(n,1);
x1(1)=x10;
x2(1)=x20;
for i=1:(n-1)            %欧拉法求解x1,x2
x1(i+1)=x1(i)+h*f1(t(i),x1(i),x2(i),c,d);
x2(i+1)=x2(i)+h*f2(t(i),x1(i),x2(i),a,b);
t(i+1)=t(i)+h;
end

``````

1个回答

MATLAB求解多元高阶微分方程组

x,y,z为位移，一阶导为速度，二阶导为加速度，m,F,c和两个角度为常数，用MATLAB求解微分方程组，![求程序e](e\1.png)

【MATLAB】常微分方程组的初值问题

1.dx/dt=cos(y),t∈[0,T] dy/dt=sin(x),t∈[0,T] x(0)=0,y(0)=0 2.用梯形公式，显式欧拉，隐式欧拉，改进欧拉求解 3.有注释再好不过 4.可以有偿

#include<iostream> #include<iomanip> using namespace std; void RK4( double(*f)( double t, double x, double y ), double(*g)( double t, double x, double y),long double initial[3], long double resu[3], double h) { long double f1,f2,f3,f4,g1,g2,g3,g4,t0,x0,y0,x1,y1; t0=initial[0];x0=initial[1];y0=initial[2]; f1=f(t0,x0,y0); f2=f(t0+h/2, x0+h*f1/2,y0+h*g1/2); f3=f(t0+h/2, x0+h*f2/2,y0+h*g2/2); f4=f(t0+h, x0+h*f3,y0+h*g3); x1=x0+h*(f1+2*f2+2*f3+f4)/6; g1=g(t0,x0,y0); g2=g(t0+h/2,x0+h*f1/2,y0+h*g1/2); g3=g(t0+h/2,x0+h*f2/2,y0+h*g2/2); g4=g(t0+h, x0+h*f3,y0+h*g3); y1=y0+h*(g1+2*g2+2*g3+g4)/6; resu[0]=t0+h;resu[1]=x1;resu[2]=y1; } int main() { double f(double t,double x, double y); double g(double t,double x, double y); long double initial[3],resu[3]; double a,b,H; double t,step; int i; cout<<"输入所求微分方程组的初值t0,x0,y0:"; cin>>initial[0]>>initial[1]>>initial[2]; cout<<"输入所求微分方程组的微分区间[a,b]:"; cin>>a>>b; cout<<"输入所求微分方程组所分解子区间的个数step:"; cin>>step; // cout<<setiosflags(ios::right)<<setiosflags(ios::fixed)<<setprecision(10); H=(b-a)/step; cout<< initial[0]<<setw(18)<<initial[1]<<setw(18)<<initial[2]<<endl; for(i=0;i<step;i++) { RK4(f,g,initial, resu,H); cout<<resu[0]<<setw(20)<<resu[1]<<setw(20)<<resu[2]<<endl; initial[0]=resu[0];initial[1]=resu[1];initial[2]=resu[2]; } return(0); } double f(double t,double x, double y) { double dx; dx=(500-x/10)*(1-x/500-y/500)*x; return(dx); } double g(double t,double x, double y) { double dy; dy=(500-y/10)*(1-y/500-x/500)*y; return(dy); }

1)改进欧拉法求解常微分方程的初值问题 #include <stdio.h> float func(float x,float y) { return(y-x); } float euler(float x0,float xn,float y0,int N) { float x,y,yp,yc,h; int i; x=x0; y=y0; h=(xn-x0)/(float)N; for(i=1;i<=N;i++) { yp=y+h*func(x,y); x=x0+i*h; yc=y+h*func(x,yp); y=(yp+yc)/2.0; } return(y); } main() { float x0,xn,y0,e; int n; printf("\ninput n:\n "); scanf("%d",&n); printf("input x0,xn:\n "); scanf("%f,%f",&x0,&xn); printf("input y0:\n "); scanf("%f",&y0); e=euler(x0,xn,y0,n); printf("y(%f)=%6.4f",y0,e); } input n: 20 input x0,xn: 1,6 input y0: 2 y(2.000000)=7.0000Press any key to continue (2)四阶龙格—库塔法 #include <stdio.h> float func(float x,float y) { return(x-y); } float runge_kutta(float x0,float xn,float y0,int N) { float x,y,y1,y2,h,xh; float d1,d2,d3,d4; int i; x=x0; y=y0; h=(xn-x0)/(float)N; for(i=1;i<=N;i++) { xh=x+h/2; d1=func(x,y); d2=func(xh,y+h*d1/2.0); d3=func(xh,y+h*d2/2.0); d4=func(xh,y+h*d3); y=y+h*(d1+2*d2+2*d3+d4)/6.0; x=x0+i*h; } return(y); } main() { float x0,xn,y0,e; int N; printf("\ninput n:\n "); scanf("%d",&N); printf("input x0,xn:\n "); scanf("%f,%f",&x0,&xn); printf("input y0:\n "); scanf("%f",&y0); e=runge_kutta(x0,xn,y0,N); printf("y(%f)=%8.6f",y0,e); } input n: 10 input x0,xn: 1,2 input y0: 5 y(5.000000)=2.833863Press any key to continue

The Euler function 欧拉公式的计算

Problem Description The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b) Input There are several test cases. Each line has two integers a, b (2<a<b<3000000). Output Output the result of (a)+ (a+1)+....+ (b) Sample Input 3 100 Sample Output 3042

Problem Description HazelFan is given two positive integers a,b， and he wants to calculate amodb. But now he forgets the value of b and only remember the value of a, please tell him the number of different possible results. Input The first line contains a positive integer T(1≤T≤5), denoting the number of test cases. For each test case: A single line contains a positive integer a(1≤a≤109). Output For each test case: A single line contains a nonnegative integer, denoting the answer. Sample Input 2 1 3 Sample Output 2 3

Problem Description The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b) Input There are several test cases. Each line has two integers a, b (2<a<b<3000000). Output Output the result of (a)+ (a+1)+....+ (b) Sample Input 3 100 Sample Output 3042

Problem Description 欧拉回路是指不令笔离开纸面，可画过图中每条边仅一次，且可以回到起点的一条回路。现给定一个图，问是否存在欧拉回路？ Input 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数，分别是节点数N ( 1 < N < 1000 )和边数M；随后的M行对应M条边，每行给出一对正整数，分别是该条边直接连通的两个节点的编号（节点从1到N编号）。当N为0时输入结 束。 Output 每个测试用例的输出占一行，若欧拉回路存在则输出1，否则输出0。 Sample Input 3 3 1 2 1 3 2 3 3 2 1 2 2 3 0 Sample Output 1 0

Java C#解决欧拉一笔画问题

Problem Description 欧拉回路是指不令笔离开纸面，可画过图中每条边仅一次，且可以回到起点的一条回路。现给定一个图，问是否存在欧拉回路？ Input 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数，分别是节点数N ( 1 < N < 1000 )和边数M；随后的M行对应M条边，每行给出一对正整数，分别是该条边直接连通的两个节点的编号（节点从1到N编号）。当N为0时输入结 束。 Output 每个测试用例的输出占一行，若欧拉回路存在则输出1，否则输出0。 Sample Input 3 3 1 2 1 3 2 3 3 2 1 2 2 3 0 Sample Output 1 0

Problem Description HazelFan is given two positive integers a,b， and he wants to calculate amodb. But now he forgets the value of b and only remember the value of a, please tell him the number of different possible results. Input The first line contains a positive integer T(1≤T≤5), denoting the number of test cases. For each test case: A single line contains a positive integer a(1≤a≤109). Output For each test case: A single line contains a nonnegative integer, denoting the answer. Sample Input 2 1 3 Sample Output 2 3

BP网络8类图片分类的简单问题

2020阿里全球数学大赛：3万名高手、4道题、2天2夜未交卷

HashMap底层实现原理，红黑树，B+树，B树的结构原理 Spring的AOP和IOC是什么？它们常见的使用场景有哪些？Spring事务，事务的属性，传播行为，数据库隔离级别 Spring和SpringMVC，MyBatis以及SpringBoot的注解分别有哪些？SpringMVC的工作原理，SpringBoot框架的优点，MyBatis框架的优点 SpringCould组件有哪些，他们...

《Oracle Java SE编程自学与面试指南》最佳学习路线图2020年最新版（进大厂必备）

《Oracle Java SE编程自学与面试指南》最佳学习路线图（2020最新版）