MATLAB基础知识【5】数据导入与绘图

137 阅读3分钟

数据导入

%加载并显示图像文件
filename = 'smile.jpg';
A = importdata(filename);
image(A);

创建文本文件weeklydata.txt

SunDay  MonDay  TuesDay  WednesDay  ThursDay  FriDay  SaturDay
95.01   76.21   61.54    40.57       55.79    70.28   81.53
73.11   45.65   79.19    93.55       75.29    69.87   74.68
60.68   41.85   92.18    91.69       81.32    90.38   74.51
48.60   82.14   73.82    41.03       0.99     67.22   93.18
89.13   44.47   57.63    89.36       13.89    19.88   46.60

创建一个脚本文件

filename = 'weeklydata.txt';
delimiterIn = ' ';
%delimiterIn: 指定数据文件中用于分隔列的字符
headerlinesIn = 1;
%headerlinesIn: 指定数据文件中包含的标题行数,这里设置为 1,表示文件的第一行是列标题
A = importdata(filename,delimiterIn,headerlinesIn);
for k = [1:7]
% 循环遍历前 7 列数据
   disp(A.colheaders{1, k})
   %显示第 k 列的列标题。使用大括号 {} 来访问单元数组中的元素
   disp(A.data(:, k))
   %显示第 k 列的所有数据。: 表示选取矩阵的所有行
   disp(' ')
   % 输出一个空行
end

绘图

函数y=x,x的取值范围为0到100,增量为5

x = [0:5:100];
y = x;
plot(x, y)

函数y = x **2

x = [-100:5:100];
y = x.^2;
plot(x, y)
x = [0:0.01:10];
y = sin(x);
plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'),
grid on, axis equal
%绘图时显示网格,并确保坐标轴比例相等

在同一图形上绘制多个函数

x= [0 : 0.01: 10];
y = sin(x);
g = cos(x);
plot(x, y, x, g, '.-'), legend('Sin(x)', 'Cos(x)')
%legend用于在图表中添加图例

设定轴比例

x = [0 : 0.01: 10];
y = exp(-x).* sin(2*x + 3);
plot(x, y), axis([0 10 -1 1])
%axis([0 10 -1 1])表示将x轴的范围设置为010,y轴的范围设置为-11

生成子图

x = [0:0.01:5];
y = exp(-1.5*x).*sin(10*x);
subplot(1,2,1)
%创建一个12列的子图网格,并激活第一个子图
plot(x,y), xlabel('x'),ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -1 1])
y = exp(-2*x).*sin(10*x);
subplot(1,2,2)
plot(x,y),xlabel('x'),ylabel('exp(–2x)*sin(10x)'),axis([0 5 -1 1])

绘制条形图

x = [1:10];
y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95];
bar(x,y), xlabel('Student'),ylabel('Score'),
title('First Sem:')
print -deps graph.eps
%生成一个名为 graph.eps 的 Encapsulated PostScript (EPS) 文件。
%EPS 是一种用于矢量图形的图像格式

绘制等高线图

[x,y] = meshgrid(-5:0.1:5,-3:0.1:3); 
%生成两个二维矩阵 x 和 y  
g = x.^2 + y.^2;  
[C, h] = contour(x,y,g);  
%生成等高线图 
%C 是等高线的矩阵
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)
%'ShowText','on' 表示在等高线上显示高度值
%文本显示的间隔是默认间隔的两倍
print -deps graph.eps

三维图

[x,y] = meshgrid(-2:.2:2);
g = x .* exp(-x.^2 - y.^2);
surf(x, y, g)
%绘制三维曲
print -deps graph.eps

求解基本代数方程

求解方程x-5 = 0中的x

roots([1, -5])
y = roots([1, -5])

Octave法求解二次方程

求解二次方程x^ 2 -7x +12 = 0。

s = roots([1, -7, 12]);

disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));

Octave中求解高阶方程

四阶方程x ^4 − 7x^ 3 + 3x^ 2 − 5x + 9 = 0

v = [1, -7,  3, -5, 9];
s = roots(v);

%将根转换为double类型
disp('Numeric value of first root'), disp(double(s(1)));
disp('Numeric value of second root'), disp(double(s(2)));
disp('Numeric value of third root'), disp(double(s(3)));
disp('Numeric value of fourth root'), disp(double(s(4)));

方程组的求解

5x + 9y = 5 3x – 6y = 4

A = [5, 9; 3, -6];
b = [5;4];
A \ b

展开和收集方程式

expand和collect分别用来展开和收集一个方程

syms x   %符号变量x
syms y   %符号变量y

%扩展方程
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(sin(2*x))
expand(cos(x+y))
 
%收集方程式
collect(x^3 *(x-7))
collect(x^4*(x-3)*(x-5))
ans =
   x^2 + 4*x - 45
ans =
   x^4 + x^3 - 43*x^2 + 23*x + 210
ans =
   2*cos(x)*sin(x)
ans =
   cos(x)*cos(y) - sin(x)*sin(y)
ans =
   x^4 - 7*x^3
ans =
   x^6 - 8*x^5 + 15*x^4
syms x
syms y
% 对 x^3 - y^3 进行因式分解
factored_expr1 = factor(x^3 - y^3)
% 分别对 x^2 - y^2 和 x^3 + y^3 进行因式分解
factored_expr2 = factor(x^2 - y^2)
factored_expr3 = factor(x^3 + y^3)
% 对 (x^4-16)/(x^2-4) 进行化简
simplified_expr = simplify((x^4-16)/(x^2-4))