MATLAB学习笔记4

  1. 变量及其操作

1.1变量命名和赋值

% 创建变量

myVariable = 10;

myVariable2 = 'Hello, MATLAB!';

1.2数组和矩阵

% 创建向量

vector = [1, 2, 3, 4];

% 创建矩阵

matrix = [1, 2, 3; 4, 5, 6; 7, 8, 9];

% 访问元素

element = matrix(1, 2); % 获取第一行第二列的元素

% 修改元素

matrix(1, 2) = 10;

% 矩阵操作

anotherMatrix = matrix * matrix; % 矩阵

1.3标准函数

% 使用标准函数

sinValue = sin(pi/2);

sqrtValue = sqrt(16);

2.绘图功能

2.1二维绘图

% 创建数据

x = 0:0.1:2*pi;

y = sin(x);

% 绘制图形

plot(x, y);

title('Sine Wave');

xlabel('Angle (radians)');

ylabel('Sin Value');

grid on;

2.2三维绘图

% 创建网格数据

X,Y\] = meshgrid(-2:0.1:2); Z = X.\*exp(-X.\^2 - Y.\^2); % 绘制三维表面图 surf(X,Y,Z); title('3D Surface Plot'); xlabel('X-axis'); ylabel('Y-axis'); zlabel('Z-axis'); 2.3设置观察点 % 创建三维数据 x = linspace(-4,4,100); y = linspace(-4,4,100); \[X,Y\] = meshgrid(x,y); Z = sin(sqrt(X.\^2 + Y.\^2)) ./ (sqrt(X.\^2 + Y.\^2)); % 绘制三维图 figure; surf(X,Y,Z); view(60,30); % 设置观察角度 3. 流程控制 3.1 条件语句 a = 10; b = 20; if a \> b disp('a is greater than b'); elseif a \< b disp('a is less than b'); else disp('a and b are equal'); end 3.2循环语句 for i = 1:5 disp(i); end count = 1; while count \<= 5 disp(count); count = count + 1; end 4.函数定义 4.1自定义函数 % 定义函数 function result = myFunction(a, b) result = a + b; end % 调用函数 result = myFunction(5, 10); disp(result); 4.2函数句柄和匿名函数 % 函数句柄 f = @sin; disp(f(pi/2)); % 相当于 sin(pi/2) % 匿名函数 g = @(x) x\^2; disp(g(5));

相关推荐
zhouping@4 分钟前
[BSidesCF 2020]Hurdles
学习·php
CODE_RabbitV4 分钟前
【保姆级实操版 - STM32 系列笔记】新手入门STM32第一课:CubeMX+Keil MDK实现LED点灯
笔记·stm32·嵌入式硬件
qiumingxun7 分钟前
【Go】Go语言基础学习(Go安装配置、基础语法)
服务器·学习·golang
H_老邪11 分钟前
Docker 学习之路-从入门到放弃:2
学习·docker·容器
hjxu201620 分钟前
【OpenClaw 龙虾养成笔记二】 自定义新的技能 Hello world
笔记·openclaw
头疼的程序员20 分钟前
计算机网络:自顶向下方法(第七版)第六章 学习分享(一)
网络·学习·计算机网络
那山川32 分钟前
ros学习笔记1~14
笔记·学习·机器人
星轨初途1 小时前
类和对象(中):六大默认成员函数与运算符重载全解析
开发语言·c++·经验分享·笔记·ajax·servlet
似水明俊德1 小时前
13-C#.Net-设计模式六大原则-学习笔记
笔记·学习·设计模式·c#·.net
Flittly1 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(11)Autonomous Agents (自治智能体)
笔记·python·ai·ai编程