台大郭彦甫MATLAB课程小练习

2023-09-23 19 0

刚开始学习,记录一下自己的练习答案,前面的忘记做了不过也都不难就不补了。

原视频链接:MATLAB教學 - 04变数(变量)与档案存取_哔哩哔哩_bilibili


P3 1:02:02(将矩阵A的值copy给B并且取绝对值)

A=[0 -1 4;9 -14 25;-34 49 64];
B=zeros(3);
for i=1:size(A,1)for j=1:size(A,2)if A(i,j)>0B(i,j)=A(i,j);elseB(i,j)=-A(i,j);endend
end
disp(A)
disp(B)

P3 1:29:53(自定义一个摄氏度华氏度转换函数)

function FtoC
while 1==1prompt = 'What is the Fahrenheit scale?';F = input(prompt);judge = isempty(F);if judge == 1breakelseC = (F-32)*5/9;str = ['The degree Celsius =',num2str(C)];disp(str)end
end

P4 0:21:42(比较字符串是否相同)

这里主要用到这几个函数:

strcmp(s1, s2):用于比较字符串s1、s2是否相等,如果相等,返回结果1,否则返回0;

strncmp(s1, s2, n):用于比较字符串s1、s2前n个字符是否相等,如果相等,返回结果1,否则返回0;

strcmpi(s1, s2):在忽略字母大小写的前提下,比较字符串s1、s2是否相等,如果相等,返回结果1,否则返回0;

strncmpi(s1, s2, n):在忽略字母大小写的前提下,比较字符串s1、s2前n个字符是否相等,如果相等,返回结果1,否则返回0。

原文链接:https://blog.csdn.net/Mrweng1996/article/details/104324094

s1='ni hao guo yan fu lao shi';
s2='ni hao guo yan fu lao shi';
s3='My name is Abrahamlin';
%比较字符串是否相等
strcmp(s1,s2)
if ans == 1str=['The conclusion of s1 compare with s2 is True'];disp(str)
elsestr=['The conclusion of s1 compare with s2 is False'];disp(str)end
strcmp(s1,s3)
if ans == 1str=['The conclusion of s1 compare with s2 is True'];disp(str)
elsestr=['The conclusion of s1 compare with s2 is False'];disp(str)
end

P4 0:22:33(反转字符串)

s1='ni hao guo yan fu lao shi';
for i = 1:length(s1)s2(i) = s1(length(s1)-i+1);if length(s2) == 25disp(s2)end
end
s1 = 'ainibaobei';
s2 = reverse(s1);
disp(s2)

P4 0:30:20(索引特定structure元素)

stduent.name = 'Anna lane';
student.id = 'aln4@xauat.edu';
student.number = '301078853';
student.grade = [95 100 90;95 82 87;100 85 100];
disp(student.grade(7));
disp(student.grade(1,3));

P4 0:46:46(构造一个cell array)

此处的string array设置需要用变量C过渡,否则会报错,原因未知

A(1,1)={'This is first cell'};
A(1,2)={[5+j*6 4+j*5]};
A(2,1)={[1 2 3;4 5 6;7 8 9]};
C = {'Tim','chris'};
A(2,2)={C};
disp(A)

P4 1:06:30/1:17:01(reshape and save/load)

A=[1:3;4:6];
B=reshape(A,3,2);
disp(B)
save exercise B
%vertify
clear
clc
load('exercise.mat')

P4 1:23:49(读取和写入XLSX文件)

A=readmatrix('exercise.xlsx');
A(:,[1])=[];
M=mean(A')';
S=std(A')';
writematrix(M,'exercise.xlsx','Sheet',1,'Range','E2:E4');
writematrix('Mean','exercise.xlsx','Sheet',1,'Range','E1');
writematrix(S,'exercise.xlsx','Sheet',1,'Range','F2:F4');
writematrix('standard','exercise.xlsx','Sheet',1,'Range','F1');

P10 0:16:42(画积分图)

a = [5 -7 5 10];
b = [4 12 -3];
c = conv(a,b);
x = -2:0.01:1;
f = polyval(c,x);
xlaber=('x');
ylaber=('f(x)/f''(x)');
plot(x,f,'--','LineWidth', 2)
hold on
g = polyder(c);
g1 = polyval(g,x);
plot(x,g1,'-','LineWidth', 2);
legend('f(x)','f''(x)');
hold off

P10 0:38:53(计算增量步为h时用diff的误差)

x0 = pi/2; h = 0.1;i = 1;
while h>0.00000009x = [x0 x0+h];y = [sin(x0) sin(x0+h)];m = diff(y)./diff(x);h = h*0.1;str = ['第',num2str(i),'次的误差=',num2str(m)];i = i+1;disp(str)
end

 这里有一个易错点,老师让求的是cos为什么我用的是sin呢?因为m = diff(y)./diff(x);这一行已经将求导公式写进去了,求出来的m已经是该点处cos的值了。


P10 0:47:52画四种情况下f(x)=e^{-x}\sin(x^2/2)的倒数的函数

hold on
for i = 1:4x = 0:power(10,-i):2*pi;y = (exp(-x)).*sin((x.^2)/2);m = diff(y)./diff(x);plot(x(1:end-1),m)
end
plot(x,y)
hold off
legend('h=0.1','h=0.01','h=0.001','h=0.0001','f(x)')

代码编程
赞赏

相关文章

UniGUI的 TUniPageControl控件动态拖动tabsheet的实现方法
mORMot 让Access的数据库可以远程访问的
UniGui中使用Grid++Report报表控件子报表获取数据的方法
Sql server日期计算常用脚本
UniGui中设置uniEdit控件的fieldLabel ,emptyText等Extjs属性
uniGUI用Grid++Report报表插件设计保存报表(For unigui ver:0.95.0.1045)