例1
Problem 1. Times 2 - START HERE
Try out this test problem first.
Given the variable x as your input, multiply it by two and put the result in y.
Examples:
Input x = 2 Output y is 4Input x = 17 Output y is 34
function y = multiply_by_two(x)
y = x * 2;
end
可以将这个函数保存在一个名为 multiply_by_two.m 的文件中,然后在 MATLAB 中调用它。
例2
Problem 44943. Calculate Amount of Cake Frosting
Given two input variables r and h, which stand for the radius and height of a cake, calculate the surface area of the cake you need to put frosting on (all around the sides and the top).
Return the result in output variable SA.
(给定两个输入变量 r 和 h,它们分别代表蛋糕的底面半径和高度,计算需要涂抹霜的蛋糕的表面积(包括侧面和顶部)。将结果存储在输出变量 SA 中。)
function SA = calculate_surface_area(r, h)
% 计算底面积
base_area = pi * r^2;
% 计算侧面积
side_area = 2 * pi * r * h;
% 总表面积
SA = base_area + side_area;
end
可以将这个函数保存在一个名为 calculate_surface_area.m 的文件中,然后在 MATLAB 中调用它。
例3
Problem 44944. Convert from Fahrenheit to Celsius
Given an input vector F containing temperature values in Fahrenheit, return an output vector C that contains the values in Celsius using the formula:
C = (F–32) * 5/9
(给定一个包含华氏温度值的输入向量 F,使用以下公式将其转换为摄氏温度值,并将结果存储在输出向量 C 中: C = (F–32) * 5/9)
function C = fahrenheit_to_celsius(F)
% 将华氏温度转换为摄氏温度
C = (F - 32) * 5/9;
end
可以将这个函数保存在一个名为 fahrenheit_to_celsius.m 的文件中,然后在 MATLAB 中调用它
例4
Problem 44947. Find the Oldest Person in a Room
Given two input vectors:
- name - user last names
- age - corresponding age of the person
Return the name of the oldest person in the output variable old_name.
(给定两个输入向量:
name - 用户的姓氏 age - 对应人的年龄 将年龄最大的人的姓氏存储在输出变量 old_name 中。)
function old_name = find_oldest_person(name, age)
% 找到年龄最大的人的姓氏
[~, idx] = max(age);
old_name = name{idx};
end
可以将这个函数保存在一个名为 find_oldest_person.m 的文件中,然后在 MATLAB 中调用它。
例5
Problem 44951. Verify Law of Large Numbers
If a large number of fair N-sided dice are rolled, the average of the simulated rolls is likely to be close to the mean of 1,2,...N i.e. the expected value of one die. For example, the expected value of a 6-sided die is 3.5.
Given N, simulate 1e8 N-sided dice rolls by creating a vector of 1e8 uniformly distributed random integers. Return the difference between the mean of this vector and the mean of integers from 1 to N.
(如果投掷大量公平的 N 面骰子,模拟投掷的平均值可能接近于1,2,...N的平均值,即一个骰子的期望值。例如,一个6面骰子的期望值是3.5。 给定N,通过创建一个包含 1e8 个均匀分布的随机整数的向量来模拟 1e8 次 N 面骰子的投掷。返回这个向量的平均值与 1 到 N 的整数平均值
之间的差异。)
function difference = simulate_dice(N)
% 模拟 N 面骰子投掷
rolls = randi([1, N], 1e8, 1); % 创建包含 1e8 个均匀分布的随机整数的向量
average_rolls = mean(rolls); % 计算向量的平均值
expected_value = (1 + N) / 2; % 计算 1 到 N 的整数平均值
difference = abs(average_rolls - expected_value); % 计算平均值之间的差异
end
可以调用这个函数并传入想要模拟的骰子的面数 N,然后它会返回模拟结果与期望值之间的差异。
1、randi(…)
randi(N) 是生成(0,N]间均匀分布的伪随机数,并且数都是整数,所以每个数是位于1到N之间。它的表达形式有以下几种:
R = randi(iMax) % 生成1:iMax之间的均匀分布随机数
R = randi(iMax,m,n) % 生成m×n的1:iMax之间的均匀分布随机数
R = randi([iMin,iMax],m,n) % 生成m×n的iMin:iMax之间的均匀分布随机数
————————————————
原文链接:https://blog.youkuaiyun.com/FX677588/article/details/72811673
2.mean()
M = mean(A)
返回沿数组中不同维的元素的平均值。
如果A是一个向量,mean(A)返回A中元素的平均值。
如果A是一个矩阵,mean(A)将其中的各列视为向量,把矩阵中的每列看成一个向量,返回一个包含每一列所有元素的平均值的行向量。
如果A是一个多元数组,mean(A)将数组中第一个非单一维的值看成一个向量,返回每个向量的平均值。
————————————————
原文链接:https://blog.youkuaiyun.com/wonengguwozai/article/details/51611270
3.Y = abs(X) 返回数组 X 中每个元素的绝对值。如果 X 是复数,则 abs(X) 返回复数的模。
例6.
Problem 44946. Solve a System of Linear Equations
Example:
If a system of linear equations in x₁ and x₂ is:
2x₁ + x₂ = 2
x₁ - 4 x₂ = 3
Then the coefficient matrix (A) is:
2 1
1 -4
And the constant vector (b) is:
2
3
To solve this system, use mldivide ( \ ):
x = A\b
Problem:
Given a constant input angle θ (theta) in radians, create the coefficient matrix (A) and constant vector (b) to solve the given system of linear equations in x₁ and x₂.
cos(θ) x₁ + sin(θ) x₂ = 1
-sin(θ) x₁ + cos(θ) x₂ = 1
(例子: 如果一个关于 x₁ 和 x₂ 的线性方程组是: 2x₁ + x₂ = 2 x₁ - 4x₂ = 3 那么系数矩阵 (A) 是: 2 1 1 -4 常数向量 (b) 是: 2 3 要解决这个方程组,使用 mldivide ( \ ): x = A\b 问题: 给定一个常数输入角度 θ(弧度制),创建系数矩阵 (A) 和常数向量 (b),以解决给定的线性方程组中的方程: cos(θ) x₁ + sin(θ) x₂ = 1 -sin(θ) x₁ + cos(θ) x₂ = 1)
% 定义输入角度 θ(弧度制)
theta = pi/4; % 例如,这里使用 π/4
% 创建系数矩阵 A
A = [cos(theta), sin(theta); -sin(theta), cos(theta)];
% 创建常数向量 b
b = [1; 1];
% 使用 mldivide ( \ ) 解决线性方程组
x = A\b;
这段程序将计算给定角度 θ(弧度制)的线性方程组的解。
例7.
Problem 44948. Calculate a Damped Sinusoid
The equa

最低0.47元/天 解锁文章
769

被折叠的 条评论
为什么被折叠?



