Matlab基础之基本数值运算
来源:互联网 发布:python windows 路径 编辑:程序博客网 时间:2024/06/10 08:19
前言:
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)%%%%%%%%%%%%%%% Untitledz = 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.8604z1 = 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 6Find 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);>>UntitledT2 = 1.9998
四、参考资料
http://pan.baidu.com/s/1hrIUcBE
- Matlab基础之基本数值运算
- //matlab数值运算
- matlab基本语法和运算基础
- matlab基本语法和运算基础
- Java基础之基本运算
- MATLAB 练习07 数值运算
- MATLAB之数值计算
- Matlab基本数值计算功能
- matlab 基本运算
- 3-MATLAB基本运算
- Matlab基本运算
- java基础之基本运算符
- matlab 矩阵基础运算
- matlab矩阵基础运算
- matlab基础运算
- JavaScript基础之数值
- matlab图像处理基础(二)之点运算
- matlab基本运算与函数
- Vsync垂直同步信号分发和SurfaceFlinger响应执行渲染流程分析(一)
- Contiki的一个demo例程
- 开博客了,和同学们一起讨论OI问题
- HDU 2487 Ugly Windows By Assassin
- 自组织神经网络SOM原理——结合例子MATLAB实现
- Matlab基础之基本数值运算
- AHandler机制
- Jlink V8固件修复
- Java面试必看二十问题
- HDU - 1108 最小公倍数
- 二叉树 层次遍历 (queue)遍历的变式题(leetcode)
- 最长递减子序列
- 牛客网刷题之旋转数组的最小数字
- HyBrid App框架