一、代数多项式法:

 tic;
clear
clc
% N=input('please key in the value of ''N''');
N=10;
M=100;
h=1/M;
X=0:h:1;
accurate_fun=inline('x.^2 - (2*exp(x))/(exp(1) + 1) - (2*exp(-x)*exp(1))/(exp(1) + 1) + 2');
f=inline('x.^2-x');
phi=inline('x.*(1-x).*x.^(i-1)','i','x');
diff_phi=inline('i*x.^(i-1)-(i+1)*x.^i','i','x');
for j=1:N
for i=1:N
A(i,j)=quad(@(x)phi(i,x).*phi(j,x)+diff_phi(i,x).*diff_phi(j,x),0,1);
end
b(j,1)=quad(@(x) phi(j,x).*f(x),0,1);
end
C=A\b;
syms x;
Un=0;
for i=1:N
Un=Un+C(i)*phi(i,x);
end
Un=Un+x;
numerical= double(vpa(subs(Un,'x',X)));
accurate=accurate_fun(X);
subplot(1,2,1)
plot(X,numerical,'r -',X,accurate,'b >');
title('numerical VS accurate');
legend('numerical','accurate');
grid on;
subplot(1,2,2);
plot(X,numerical-accurate,'g');
title('error');
grid on;
toc;

二、三角函数法:

 tic;
clear
clc
% N=input('please key in the value of ''N''');
N=10;
M=100;
h=1/M;
X=0:h:1;
accurate_fun=inline('x.^2 - (2*exp(x))/(exp(1) + 1) - (2*exp(-x)*exp(1))/(exp(1) + 1) + 2');
f=inline('x.^2-x');
phi=inline('sin(i*pi*x)','i','x');
diff_phi=inline('i*pi*cos(i*pi*x)','i','x');
for j=1:N
for i=1:N
A(i,j)=quad(@(x)phi(i,x).*phi(j,x)+diff_phi(i,x).*diff_phi(j,x),0,1);
end
b(j,1)=quad(@(x) phi(j,x).*f(x),0,1);
end
C=A\b;
syms x;
Wn=0;
for i=1:N
Wn=Wn+C(i)*phi(i,x);
end
Un=Wn+x;
numerical=vpa(subs(Un,'x',X));
accurate=accurate_fun(X);
subplot(1,2,1)
plot(X,numerical,'r -',X,accurate,'g ^');
title('Ritz Galerkin method');
legend('numerical solution','accurate solution');
grid on;
subplot(1,2,2)
plot(X,numerical-accurate,'b');
title('error');
grid on;
toc;

Matlab-10:Ritz-Galerkin方法求解二阶常微分方程的更多相关文章

  1. FESTUNG — 3. 采用 HDG 方法求解对流问题

    FESTUNG - 3. 采用 HDG 方法求解对流问题[1] 1. 控制方程 线性对流问题控制方程为 \[\begin{array}{ll} \partial_t c + \nabla \cdot ...

  2. matlab中struct创建方法

    MATLAB中struct创建方法可分为:直接创建法和struct()函数创建法 (1)直接创建: 直接定义字段,像使用一般matlab变量一样,不需要事先声明,支持动态扩充.下面创建一个Studen ...

  3. Matlab人脸检測方法(Face Parts Detection)具体解释

    今天同学让我帮忙制作一个人脸表情识别的样本库,当中主要是对人脸进行裁剪,这里用到了一个相对较新的Matlab人脸检測方法Face Parts Detection.网上百度了一下发现关于Matlab人脸 ...

  4. Effective Java 第三版——10. 重写equals方法时遵守通用约定

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  5. C语言多种方法求解字符串编辑距离问题的代码

    把做工程过程经常用的内容记录起来,如下内容段是关于C语言多种方法求解字符串编辑距离问题的内容. { if(xbeg > xend) { if(ybeg > yend) return 0; ...

  6. 三种初步简易的方法求解数值问题 of C++

    1. “二分法解方程” 在二分法中,从区间[a,b]开始,用函数值f(a)与f(b)拥有相反的符号.如果f在这个区间连续,则f的图像至少在x=a,x=b之间穿越x轴一次,因此方程f(x)=0在[a,b ...

  7. 在vs2010中编译log4cxx-0.10.0详细方法(从下载、编译、解决错误详细介绍)

    在vs2010中编译log4cxx-0.10.0详细方法(从下载.编译.解决错误详细介绍) http://blog.sina.com.cn/s/blog_a459dcf501013tbn.html

  8. Python中list常用的10个基本方法----list的灰魔法

    ########################list 的常用的10个基本方法################################## list 类 列表# 1 列表的基本格式#2 可以 ...

  9. 从Ubuntu 18.04 LTS升级到Ubuntu 18.10版本的方法

    从Ubuntu 18.04 LTS升级到Ubuntu 18.10版本的方法 2018-10-18 21:08:39作者:ywnz稿源:云网牛站 本文提供从Ubuntu 18.04 LTS(Bionic ...

随机推荐

  1. IDEA查看一个类的所有继承关系

    通常一个.java文件对应一个java类. 鼠标右击一个类: 即可查看.按住alt键可放大. 另一快捷键:光标在类名上,ctrl+H

  2. 题解——CodeForces 438D The Child and Sequence

    题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...

  3. Thread类的常用方法

    String getName() 返回该线程的名称. void setName(String name) 改变线程名称,使之与参数 name 相同. int getPriority() 返回线程的优先 ...

  4. 利用mysql行级锁创建数据库主键id

    存储函数: CREATE FUNCTION `getSerialNo`(`serialName` VARCHAR(50), `skip` INT) RETURNS bigint(20) COMMENT ...

  5. VHDL 数字时钟设计

    序言 这个是我在做FPGA界的HelloWorld--数字钟设计时随手写下的,再现了数字钟设计的过程 目标分析 时钟具有时分秒的显示,需6个数码管.为了减小功耗采用扫描法显示 按键设置时间,需要对按键 ...

  6. HDU 5242 Game(贪心)

    http://acm.hdu.edu.cn/showproblem.php?pid=5242 题意: 给出一棵树,每个节点都有一个权值,每次可以获得从根结点(1)到叶子节点上的所有权值和,每个节点只能 ...

  7. 线性判别分析(Linear Discriminant Analysis-LDA)

    Linear Discriminant Analysis(LDA线性判别分析) 用途:数据预处理中的降维,分类任务 目标:LDA关心的是能够最大化类间区分度的坐标轴成分,将特征空间(数据集中的多维样本 ...

  8. Oracle DBHelper 第二版

    public static class OracleDBHelper { public static OracleCommand cmd = null; public static OracleCon ...

  9. 数据库备份出现警告:Warning: Using a password on the command line interface can be insecure. Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even thos

    1.先来看原备份数据库语句: mysqldump -h 127.0.0.1 -uroot -ppassword database > /usr/microStorage/dbbackup/cap ...

  10. python 安装pip

    https://pip.pypa.io/en/stable/installing/