Matlab基础之基本数值运算
2016-10-04 15:05
239 查看
前言:
Matlab数值运算包罗万象,丰富多彩,本笔记只介绍其数值运算的常用一小部分。
;
比如
, 对应在Matlab中 a=[2 3 4 2 59 8];
拟合:polyfit()函数,不一定用离散点建立多项式函数关系,但是建立的多项式要与离散点误差(平方和距离)最小。
在实际中,若果我们得到的离散数据点比较精确,那么就用差值方法,否则相反。
而差值函数polyfit()和polyval()结合使用。
polyfit(
X = [1 1 2 3 5 8 13 21];
Y = diff(X)
Y =
0 1 1 2 3 5 8
Y = diff(X,n);Create a vector and compute the second-order difference between the elements.创建二阶差分
X = [0 5 15 30 50 75 105];
Y = diff(X,2)
Y =
5 5 5 5 5
练习:
计算
在
区间的微分。
y = linspace(x1,x2,n) generates n points. The spacing between the points is (x2-x1)/(n-1).在区间[x1,x2]中产生n个点,分成n-1个区间,每个区间步长 (x2-x1)/(n-1).
example:
y1 = linspace(-5,5,7)
y1 =
-5.0000 -3.3333 -1.6667 0 1.6667 3.3333 5.0000
2.函数cumsum():
B = cumsum(A)
B = cumsum(A,dim)
B = cumsum(___,direction)
Description:
B = cumsum(A) returns the cumulative sum of A starting at the beginning of the first array dimension in A whose size does not equal 1.
If A is a vector, then cumsum(A) returns a vector containing the cumulative sum of the elements of A.
如果A是一个向量,返回的是向量A元素累积和
若果A是矩阵,返回的新矩阵是矩阵A列的累积和
综合举例:对f(x)求积分
Matlab数值运算包罗万象,丰富多彩,本笔记只介绍其数值运算的常用一小部分。
一、多项式运算
多项式一般类型:;
比如
, 对应在Matlab中 a=[2 3 4 2 59 8];
>> a=[2 3 4 2 59 8]; b=[12 2 4 3 5 7 8 9 1]; >> polyval(a,1)%当x=1时,多项式的值 ans = 78 >>c=conv(a,b)%多项式a与b相乘(卷积) c= 24 40 62 50 747 263 315 289 394 508 550 597 131 8 >>deconv(c,b)%多项式相除(解卷积) d = 2.0000 3.0000 4.0000 2.0000 59.0000 8.0000 >>roots(a)%当多项式a=0时,x的值 ans = -1.8991 + 1.7358i -1.8991 - 1.7358i 1.2172 + 1.7203i 1.2172 - 1.7203i -0.1361 + 0.0000i
二、插值与拟合
差值:interp1()函数,利用已有的一系列离散点建立变量之间的多项式函数关系。拟合:polyfit()函数,不一定用离散点建立多项式函数关系,但是建立的多项式要与离散点误差(平方和距离)最小。
在实际中,若果我们得到的离散数据点比较精确,那么就用差值方法,否则相反。
a=[1 3 4 2 59 8 40 30 ]; b=[12 20 40 31 1 54 10 50 ]; x=1:0.2:60; y1=interp1(a,b,x,'linear');%两种插值方式比较 y2=interp1(a,b,x,'cubic'); %subplot(2,1,1); plot(x,y1,'r','LineWidth',2); hold on; %subplot(2,1,2); plot(x,y2,'b','LineWidth',1); title('曲线'); z=interp1(a,b,2.3) %%%%%%%%%%%%%%% Untitled z = 27.7000
而差值函数polyfit()和polyval()结合使用。
p=
polyfit(
x,
y,
n),n是多项式P的n次差值,返回的是多项式P。
a=[1 3 4 2 59 8 40 30 ]; b=[12 20 40 31 1 54 10 50 ]; P=polyfit(a,b,5); z2=polyval(P,2.3) z1=interp1(a,b,2.3) %%%%%%%%%%% >> Untitled%两者有区别的 z2 = 24.8604 z1 = 27.7000
三、数值微分
函数Y = diff(X);Create a vector, then compute the differences between the elements.创建一阶差分X = [1 1 2 3 5 8 13 21];
Y = diff(X)
Y =
0 1 1 2 3 5 8
Y = diff(X,n);Create a vector and compute the second-order difference between the elements.创建二阶差分
X = [0 5 15 30 50 75 105];
Y = diff(X,2)
Y =
5 5 5 5 5
x=-2*pi:0.2:2*pi; y=sin(x); plot(x,y,'o'); hold on; dx=diff(x); dy=diff(y); diff_f=diff(y)./diff(x);%求导数 plot(-2*pi:0.2:(2*pi-0.2),diff_f,'r','LineWidth',2);
练习:
计算
在
区间的微分。
a=[1 -3 -8 7 3 -5]; x=-4:0.1:5; plot(x,polyval(a,x),'r'); hold on; dx=diff(x); dy=diff(polyval(a,x)); diff_f=dy./dx; x1=(-4:0.1:4.9); plot(x1,diff_f);
三、数值积分
1.函数linspace():y = linspace(x1,x2,n) generates n points. The spacing between the points is (x2-x1)/(n-1).在区间[x1,x2]中产生n个点,分成n-1个区间,每个区间步长 (x2-x1)/(n-1).
example:
y1 = linspace(-5,5,7)
y1 =
-5.0000 -3.3333 -1.6667 0 1.6667 3.3333 5.0000
2.函数cumsum():
B = cumsum(A)
B = cumsum(A,dim)
B = cumsum(___,direction)
Description:
B = cumsum(A) returns the cumulative sum of A starting at the beginning of the first array dimension in A whose size does not equal 1.
If A is a vector, then cumsum(A) returns a vector containing the cumulative sum of the elements of A.
如果A是一个向量,返回的是向量A元素累积和
A = 1:5; B = cumsum(A) B = 1 3 6 10 15If A is a matrix, then cumsum(A) returns a matrix containing the cumulative sums for each column of A.
若果A是矩阵,返回的新矩阵是矩阵A列的累积和
A = [1 3 5; 2 4 6] A 1 3 5 2 4 6 Find the cumulative sum of the rows of A. >>B = cumsum(A,2)%对列进行累计求和 B = 1 4 9 2 6 12 >>B = cumsum(A,1)%对行进行累计求和 B = 1 3 5 3 7 11If A is a multidimensional array, then cumsum(A) acts along the first nonsingleton dimension.巴拉巴拉。。。。
综合举例:对f(x)求积分
x=linspace(0,pi,100); y=sin(x); T1=cumsum(y)*(pi-0)/(100-1); T2=T1(100); >>Untitled T2 = 1.9998
四、参考资料
http://pan.baidu.com/s/1hrIUcBE相关文章推荐
- 矩阵论基础 2.5 用Matlab实现矩阵的基本运算
- matlab基本语法和运算基础
- matlab基本语法和运算基础
- matlab基本语法和运算基础
- 【基础算法】位运算-基本运算
- javascipt——基础知识——基本数据类型和逻辑运算
- Matlab矩阵基本操作(定义,运算)
- Matlab入门教程--基本运算与函数(一)
- LinuxShell脚本编程基础2-变量与数值运算、父shell和子shell
- C语言基础—基本运算
- matlab 数组矩阵基本运算
- java基础-数值运算中的类型转换问题
- java基础复习1--基本数据类型、运算符号、表达式语句与程序结构
- 【零基础学习iOS开发】【02-C语言】08-基本运算
- Matlab矩阵基本操作(定义,运算)
- MATLAB基本操作(五):MATLAB中的逻辑运算及all(),any()的使用
- C基础——基本运算
- matlab基本运算与函数
- [Matlab]基础教程学习笔记(七):符号运算
- R语言基础入门3--数值运算的R函数