1934 0

[Weka及其他] MATLAB课程:代码示例之Mathematics(三) [推广有奖]

企业贵宾

已卖:160份资源

巨擘

0%

还不是VIP/贵宾

-

威望
4
论坛币
624047 个
通用积分
180.4857
学术水平
918 点
热心指数
987 点
信用等级
841 点
经验
399173 点
帖子
9786
精华
48
在线时间
17322 小时
注册时间
2014-8-19
最后登录
2022-11-2

楼主
widen我的世界 学生认证  发表于 2016-3-7 12:41:01 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

MATLAB课程:代码示例之Mathematics(三)

Single Precision Math


This example shows how to perform arithmetic and linear algebra with single precision data. It also shows how the results are computed appropriately in single-precision or double-precision, depending on the input.


Create Double Precision Data

Let's first create some data, which is double precision by default.

Ad = [1 2 0; 2 5 -1; 4 10 -1]


Ad =     1     2     0     2     5    -1     4    10    -1


Convert to Single Precision

We can convert data to single precision with the single function.

A = single(Ad); % or A = cast(Ad,'single');


Create Single Precision Zeros and Ones

We can also create single precision zeros and ones with their respective functions.

n = 1000;Z = zeros(n,1,'single');O = ones(n,1,'single');


Let's look at the variables in the workspace.

whos A Ad O Z n


  Name         Size            Bytes  Class     Attributes  A            3x3                36  single                Ad           3x3                72  double                O         1000x1              4000  single                Z         1000x1              4000  single                n            1x1                 8  double              


We can see that some of the variables are of type single and that the variable A (the single precision version of Ad) takes half the number of bytes of memory to store because singles require just four bytes (32-bits), whereas doubles require 8 bytes (64-bits).

Arithmetic and Linear Algebra

We can perform standard arithmetic and linear algebra on singles.

B = A'    % Matrix Transpose


B =     1     2     4     2     5    10     0    -1    -1


whos B


  Name      Size            Bytes  Class     Attributes  B         3x3                36  single              


We see the result of this operation, B, is a single.

C = A * B % Matrix multiplication


C =     5    12    24    12    30    59    24    59   117


C = A .* B % Elementwise arithmetic


C =     1     4     0     4    25   -10     0   -10     1


X = inv(A) % Matrix inverse


X =     5     2    -2    -2    -1     1     0    -2     1


I = inv(A) * A % Confirm result is identity matrix


I =     1     0     0     0     1     0     0     0     1


I = A \ A  % Better way to do matrix division than inv


I =     1     0     0     0     1     0     0     0     1


E = eig(A) % Eigenvalues


E =    3.7321    0.2679    1.0000


F = fft(A(:,1)) % FFT


F =   7.0000 + 0.0000i  -2.0000 + 1.7321i  -2.0000 - 1.7321i


S = svd(A) % Singular value decomposition


S =   12.3171    0.5149    0.1577


P = round(poly(A)) % The characteristic polynomial of a matrix


P =     1    -5     5    -1


R = roots(P) % Roots of a polynomial


R =    3.7321    1.0000    0.2679


Q = conv(P,P) % Convolve two vectorsR = conv(P,Q)


Q =     1   -10    35   -52    35   -10     1R =     1   -15    90  -278   480  -480   278   -90    15    -1


stem(R); % Plot the result


A Program that Works for Either Single or Double Precision

Now let's look at a function to compute enough terms in the Fibonacci sequence so the ratio is less than the correct machine epsilon (eps) for datatype single or double.

% How many terms needed to get single precision results?fibodemo('single')% How many terms needed to get double precision results?fibodemo('double')% Now let's look at the working code.type fibodemo% Notice that we initialize several of our variables, |fcurrent|,% |fnext|, and |goldenMean|, with values that are dependent on the% input datatype, and the tolerance |tol| depends on that type as% well.  Single precision requires that we calculate fewer terms than% the equivalent double precision calculation.


ans =    19ans =    41function nterms = fibodemo(dtype)%FIBODEMO Used by SINGLEMATH demo.% Calculate number of terms in Fibonacci sequence.% Copyright 1984-2014 The MathWorks, Inc.fcurrent = ones(dtype);fnext = fcurrent;goldenMean = (ones(dtype)+sqrt(5))/2;tol = eps(goldenMean);nterms = 2;while abs(fnext/fcurrent - goldenMean) >= tol   nterms = nterms + 1;   temp  = fnext;   fnext = fnext + fcurrent;   fcurrent = temp;end




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:mathematics Mathematic MATLAB课程 Thematic MATLAB MATLAB课程 代码示例 Mathematics SinglePrecisionMath

本帖被以下文库推荐


https://www.cda.cn/?seo-luntan
高薪就业·数据科学人才·16年教育品牌

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 13:03