一、代数多项式法:

 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. WebLogic调用WebService提示Failed to localize、Failed to create WsdlDefinitionFeature

    在本地Tomcat环境下调用WebService正常,但是部署到WebLogic环境中,则提示警告:[Failed to localize] MEX0008.PARSING_MDATA_FAILURE ...

  2. p3396 哈希冲突(暴力)

    想了好久,没想到优秀的解法,结果是个暴力大吃一静.jpg 分类讨论,预处理\(p\le \sqrt{n}\) 的情况,其他直接暴力,复杂度\(O(n \sqrt{n} )\) #include < ...

  3. (转) Dissecting Reinforcement Learning-Part.2

    Dissecting Reinforcement Learning-Part.2 Jan 15, 2017 • Massimiliano Patacchiola 原文链接:https://mpatac ...

  4. iframe初始化属性

    <iframe id="user" src="xxx.html" frameborder="0" width="" ...

  5. Jdbc -Statement

    Java提供了 Statement.PreparedStatement 和 CallableStatement三种方式来执行查询语句: PreparedStatement是用于执行参数化查询 预编译s ...

  6. linux 打开一个文件现swap文件

    转自:http://blog.csdn.net/eckelwei/article/details/17078187 有时候在用vim打开文件时提示类似以下的信息: 发现交换文件 ".expo ...

  7. eclipse配置tomcat后出现:java virtual machine launcher Error:Could not ……,Program will exit.

    原贴在stack overflow上:https://stackoverflow.com/questions/50085750/apache-tomcat-9-x-not-working-with-e ...

  8. eclipse导入项目文件以及 import项目文件后有个红色感叹号

    eclipse导入项目文件 步骤:File —>Import—>General—>Existing Projects into Workspace 然后进去选择项目文件的具体路径即可 ...

  9. C#数组维数及不同维数中元素个数的获取

    简单理解有关数组维数的概念: 1.编程中用到的多维的数组,最多也就是二维数组了 2.数组的维数从0开始计算 using System; using System.Collections.Generic ...

  10. Asp.net core 学习笔记 ( ef core )

    更新 : 2018-11-26 这里记入一下关于 foreignKey cascade action 默认情况下如果我们使用 data annotation required + foreginkey ...