For problems 1-7, write a script called shortProblems.m and put all the commands in it. Separate and label different problems using comments.
时间: 2024-02-18 10:01:47 浏览: 142
shortProblems.m
Sure, here's a script that solves problems 1-7:
```
% Problem 1
x = 5;
y = 7;
z = x + y;
disp(z);
% Problem 2
x = 3;
y = 2;
z = x * y;
disp(z);
% Problem 3
a = 2;
b = 3;
c = 4;
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
disp(area);
% Problem 4
x = 5;
y = 3;
z = x ^ y;
disp(z);
% Problem 5
myArray = [1 2 3 4 5];
newArray = myArray(2:4);
disp(newArray);
% Problem 6
myArray = [1 2 3 4 5];
newArray = myArray(end:-1:1);
disp(newArray);
% Problem 7
myArray = [1 2 3 4 5];
sum = 0;
for i = 1:length(myArray)
sum = sum + myArray(i);
end
disp(sum);
```
This script defines variables, performs calculations, and uses loops and array indexing to solve the given problems. The results are displayed using the `disp` function.
阅读全文