關(guān)于取MATLAB的有效位數(shù)問題,以及vpa函數(shù)

vpa函數(shù)

設(shè)用于設(shè)置數(shù)據(jù)的有效位數(shù), 但是如果用這個(gè)方式處理數(shù)據(jù)后返回的則是 sys變量, 處理很不方便

digits()
設(shè)置了以后,后面的數(shù)據(jù)都是按這種設(shè)置的顯示

以下轉(zhuǎn)自于 http://blog.csdn.net/yf210yf

format:設(shè)置輸出格式 對浮點(diǎn)性變量,缺省為format short. format并不影響matlab如何計(jì)算和存儲(chǔ)變量的值。對浮點(diǎn)型變量的計(jì)算, 即單精度或雙精度,按合適的浮點(diǎn)精度進(jìn)行,而不論變量是如何顯示的。對整型變量采用整型數(shù)據(jù)。 整型變量總是根據(jù)不同的類(class)以合適的數(shù)據(jù)位顯示,例如,3位數(shù)字顯示顯示int8范圍 -128:127。format short, long不影響整型變量的顯示。 format long 顯示15位雙精度,7為單精度(scaled fixed point) format short 顯示5位(scaled fixed point format with 5 digits) format short eng 至少5位加3位指數(shù) format long eng 16位加至少3位指數(shù) format hex 十六進(jìn)制 format bank 2個(gè)十進(jìn)制位 format + 正、負(fù)或零 format rat 有理數(shù)近似 format short 缺省顯示 format long g 對雙精度,顯示15位定點(diǎn)或浮點(diǎn)格式,對單精度,顯示7位定點(diǎn)或浮點(diǎn)格式。 format short g 5位定點(diǎn)或浮點(diǎn)格式 format short e 5位浮點(diǎn)格式 format long e 雙精度為15位浮點(diǎn)格式,單精度為7為浮點(diǎn)格式 Matlab里面顯示的數(shù)字默認(rèn)情況下是以short類型進(jìn)行顯示和存儲(chǔ)的。但是有時(shí)候我們需要對它的顯示格式(精度)進(jìn)行更改,以適合我們的需求。更改方法如下:

a=1.23456789  

b=vpa(a,7) 

b = 1.234568  

vpa(a,12)  

ans = 1.23456789000

Matlab取整函數(shù)有: fix, floor, ceil, round.取整函數(shù)在編程時(shí)有很大用處。

一、取整函數(shù)

1.向零取整(截尾取整)

fix-向零取整(Round towards zero);

fix(3.6)

ans = 3

2.向負(fù)無窮取整(不超過x 的最大整數(shù)-高斯取整)

floor-向負(fù)無窮取整(Round towards minus infinity);

floor(-3.6)

ans = -4

3.向正無窮取整(大于x 的最小整數(shù))

ceil-向正無窮取整(Round towards plus infinity);

ceil(-3.6)

ans = -3

4.向最近整數(shù)取整,四舍五入(四舍五入取整)

round-向最近整數(shù)取整,四舍五入(Round towards nearest integer);

round(3.5)

ans = 4

二、在小數(shù)點(diǎn)后某一位四舍五入,即保留幾位小數(shù),也經(jīng)常用到。

1.數(shù)值型 roundn—任意位位置四舍五入

a=123.4567890;

a=roundn(a,-4)

a = 123.4568

其中roundn函數(shù)功能如下:

y = ROUNDN(x) rounds the input data x to the nearest hundredth. %不指定n,精確到百分位 y = ROUNDN(x,n) rounds the input data x at the specified power %精確到小數(shù)點(diǎn)后指定位數(shù)n

2.符號(hào)型

digits(4)

vpa(....)

必須說明:vpa命令不能識(shí)別整數(shù)與小數(shù),只算總位數(shù),因此對它來說小數(shù)整數(shù)無論哪個(gè)都占一位,例如對9.3154保留兩位小數(shù)時(shí)就得寫成:

a=9.3154;

digits(3)

b=vpa(a)

b= 9.32

其中b為符號(hào)型變量;

3.字符型

a=12.34567;

b = sprintf('%8.2f',a)

b = 12.35 其中b為字符型變量。

matlab文本輸出

兩個(gè)函數(shù):disp

      fprintf

1、函數(shù)disp只帶一個(gè)變量,他可以是自負(fù)矩陣或數(shù)值矩陣,要輸出簡單的文字信息,只需要用單引號(hào)將信息括起來:

disp(‘my favorite color is red’);

或者

yourname=input(‘enter your name’,’s’);

disp([‘your name is’,youname]);

例如

yourname = input('enter your name ','s');

enter your name panrq

disp(['your name is ',yourname]);

your name is panrq

選擇帶數(shù)值變量值的文本信息時(shí),需要用函數(shù)num2str將數(shù)值變量的類型轉(zhuǎn)換字符型

x=98;

outstring = ['x = ',num2str(x)];

disp(outstring);

x = 98

disp(['x = ',num2str(x)]);

x = 98

disp函數(shù)只能帶一個(gè)變量,表格中的各列需奧組合成一個(gè)矩陣,如下面的程序所示。

x=0:pi/5:pi;y=sin(x);

disp([x' y']);

     0         0

0.6283    0.5878

1.2566    0.9511

1.8850    0.9511

2.5133    0.5878

3.1416    0.0000

Format命令

控制顯示模式,直到下一個(gè)format出現(xiàn)前,這條format命令一直有效。

x=1.23456789;

format short;disp(pi);

3.1416

format long;disp(pi);

3.141592653589793

format short e;disp(pi);

3.1416e+000

format +;disp(pi);

format bank;disp(pi);

      3.14

2、函數(shù)fprintf

fprintf(format);

fprintf(format,variables);

fprintf(fid,format,variables);

例如:

fprintf('i am concreten');

i am concrete

a=3;b='s';

fprintf('this is a %d and %s n',a,b);

this is a 3 and s

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容