台大郭彦甫MATLAB课程小练习

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

原视频链接: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)>0
            B(i,j)=A(i,j);
        else
            B(i,j)=-A(i,j);
        end
    end
end
disp(A)
disp(B)

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

function FtoC
while 1==1
    prompt = 'What is the Fahrenheit scale?';
    F = input(prompt);
    judge = isempty(F);
    if judge == 1
        break
    else
        C = (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 == 1
    str=['The conclusion of s1 compare with s2 is True'];
    disp(str)
else
    str=['The conclusion of s1 compare with s2 is False'];
    disp(str)
    
end
strcmp(s1,s3)
if ans == 1
    str=['The conclusion of s1 compare with s2 is True'];
    disp(str)
else
    str=['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) == 25
        disp(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.00000009
    x = [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:4
    x = 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)')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不自律的狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值