



大一MATLAB题,总共十一道,实在是做得焦头烂额,辛苦各位了
function [palindromeCount, nonPalindromeCount] = palindromeGenerator(limit)
palindromeCount = 0;
nonPalindromeCount = 0;
for i = 1:limit
if isPalindromeGenerator(i, 6)
palindromeCount = palindromeCount + 1;
else
nonPalindromeCount = nonPalindromeCount + 1;
end
end
end
function isPalindrome = isPalindromeGenerator(num, steps)
for i = 1:steps
num = num + str2num(flip(num2str(num)));
if isPalindrome(num)
isPalindrome = true;
return;
end
end
isPalindrome = false;
end
function result = isPalindrome(num)
str = num2str(num);
result = strcmp(str, flip(str));
end
function perfectNumbers = findPerfectNumbers(limit)
perfectNumbers = [];
for n = 1:limit
if isPerfect(n)
perfectNumbers(end + 1) = n;
end
end
end
function result = isPerfect(number)
sumOfDivisors = sum(divisors(number)) - number;
result = sumOfDivisors == number;
end
function divs = divisors(n)
divs = [1];
for i = 2:sqrt(n)
if mod(n, i) == 0
divs(end+1) = i;
if i ~= n / i
divs(end+1) = n / i;
end
end
end
end
function amicablePairs = findAmicablePairs(limit)
amicablePairs = [];
for a = 1:limit
b = sum(divisors(a)) - a;
if a < b && sum(divisors(b)) - b == a
amicablePairs(end+1, :) = [a, b];
end
end
end
function divs = divisors(n)
divs = [1];
for i = 2:sqrt(n)
if mod(n, i) == 0
divs(end+1) = i;
if i ~= n / i
divs(end+1) = n / i;
end
end
end
end
function [prime1, prime2] = goldbachConjecture(evenNumber)
for i = 2:evenNumber-2
if isPrime(i) && isPrime(evenNumber-i)
prime1 = i;
prime2 = evenNumber - i;
return;
end
end
end
function result = isPrime(n)
if n <= 1
result = false;
elseif n <= 3
result = true;
else
result = all(mod(n, 2:floor(sqrt(n))) ~= 0);
end
end
function stats = calculateGradeStats(grades)
n = length(grades);
total = sum(grades);
avg = mean(grades);
countBelow60 = sum(grades < 60);
count60to70 = sum(grades >= 60 & grades < 70);
count70to80 = sum(grades >= 70 & grades < 80);
count80to90 = sum(grades >= 80 & grades < 90);
countAbove90 = sum(grades >= 90);
stats = struct('TotalStudents', n, 'TotalScore', total, 'AverageScore', avg, ...
'CountBelow60', countBelow60, 'Count60to70', count60to70, ...
'Count70to80', count70to80, 'Count80to90', count80to90, ...
'CountAbove90', countAbove90);
end
function guessNumberGame()
randomNumber = randi([1, 100]);
attempts = 7;
for i = 1:attempts
guess = input('Guess a number between 1 and 100: ');
if guess == randomNumber
disp('You win!');
return;
elseif guess > randomNumber
disp('High');
else
disp('Low');
end
end
disp(['Game over. The number was ', num2str(randomNumber
)]);
end
function zodiac = findZodiac(month, day)
zodiacDates = [120, 219, 321, 420, 521, 621, 723, 823, 923, 1023, 1122, 1222, 1231; ...
218, 320, 420, 520, 620, 722, 822, 922, 1022, 1121, 1221, 1231, 119];
zodiacs = {'水瓶座', '双鱼座', '白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', ...
'天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座'};
dateNum = month * 100 + day;
for i = 1:length(zodiacDates)-1
if dateNum >= zodiacDates(1, i) && dateNum <= zodiacDates(2, i)
zodiac = zodiacs{i};
return;
end
end
end
% (1) z=(xy)/(x²+y²), x²+y²≠0
[x, y] = meshgrid(-2:0.1:2, -2:0.1:2);
z = (x .* y) ./ (x.^2 + y.^2);
z(x.^2 + y.^2 == 0) = 0;
mesh(x, y, z);
% (2) z=(1+x+y)²
z = (1 + x + y).^2;
mesh(x, y, z);
% (3) z=√(x²+y²)
z = sqrt(x.^2 + y.^2);
mesh(x, y, z);
% (4) y=z²+1 绕z轴旋转
z = linspace(-2, 2, 100);
r = sqrt(z.^2 + 1);
[X, Y, Z] = cylinder(r);
surf(X, Y, Z * 4 - 2);
[x, y] = meshgrid(-8:0.2:8, -8:0.2:8);
z = (sin(x.^2 + y.^2)) ./ sqrt(x.^2 + y.^2);
mesh(x, y, z); % 使用 mesh
meshc(x, y, z); % 使用 meshc
meshz(x, y, z); % 使用 meshz
% (1) 2z=x²+y², z=1
[x, y] = meshgrid(-2:0.1:2, -2:0.1:2);
z = (x.^2 + y.^2) / 2;
z1 = ones(size(z));
surf(x, y, z);
hold on;
surf(x, y, z1);
hold off;
% 其他部分的代码可以类似编写,根据具体的数学表达式调整。
% 球面 x²+y²+z²=4
[x, y, z] = sphere;
surf(2*x, 2*y, 2*z);
% 球面 x²+y²+(z-2)²=4
surf(2*x, 2*y, 2*(z+1));