matlab学习笔记

Posted by wzc on 2020-08-24

matlab学习笔记

字符串处理:p24

​ strcat(x,y):连接字符串

​ strrep(x,’who’,y):用y替换x中的who

​ lower():修改字符串大小写

​ strjust(A,’left’):将A字符位置调至左边

matlab中

单元数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function grade_assess(Name,Score)
%UNTITLED4 此处显示有关此函数的摘要
% Name中的元素为学生姓名
% Score中元素为学分制
% 统计学生人数
n=length(Name);
% 将分数区间划分开,这里创建了三个单元数组,A等级数组数有16个,B等级元素数有15个,C等级有10个
% 每个都是一个单独的个体
for i=0:15
A_level{i+1}=85+i;
if i<=14
B_level{i+1}=70+i;
if(i<=9)
C_level{i+1}=60+i;
end
end
end
% 创建存储成绩等级的数组
Level=cell(1,n);
% 创建结构体S
% 结构体必须有对应的行数和列数,这里Name和Score和Level都为1xn的数组,使用机构体后相当于
% 构成了nx1的数组,通过索引即可访问一条记录
S=struct('Name',Name,'Score',Score,'Level',Level);
% 使用switch比较时,个人理解为switch后面的值与case中数组每个都比较一下,存在则为真
for i=1:n
switch S(i).Score
case A_level
S(i).Level='优';
case B_level
S(i).Level='良';
case C_level
S(i).Level='及格';
otherwise
S(i).Level='不及格';
end
end
disp(A_level);
disp(S);
%显示所有学生的成绩等级评定
disp(['学生姓名',blanks(4),'得分',blanks(4),'等级']);
for i=1:n
disp([S(i).Name,blanks(4),num2str(S(i).Score),blankscheng(6),S(i).Level]);
end
%disp只能输出字符串
end
%

程序的注解:

1、disp命令

​ 该命令用来展示变量的内容,可以是数值、字符串或表达式,可以输出几乎所有类型的变量。它的使用格式如下:

disp(X):在屏幕上显示任何输入的变量

2、input命令

​ 该命令用来提示用户从键盘输入数值、字符串或表达式,并将相应的值赋给指定的变量。

3、keyboard命令

​ 该命令是一个键盘调用命令,即当在一个M文件中运行该命令后,该文件将停止执行并将“控制权”交给键盘,产生一个以K开头的提示符(K>>),用户可以通过键盘输入各种METLAB的合法命令。只有当输入return命令时,程序才将“控制权”交给原文件。

4、menu命令

​ 该命令用来产生一个菜单供用户选择,它的使用格式为:

k=menu(‘mtitle’,‘opt1’,‘opt2’,…,‘optn’)

符号运算

符号对象的建立:sym syms

sym 函数用来建立单个符号变量,一般调用格式为:符号变量 = sym(A)参数 A 可以是一个数或数值矩阵,也可以是字符串

例如:a=sym(‘a’) a 是符号变量

b=sym(1/3) b 是符号常量

C=sym(‘[1 ab; c d]’) C 是符号矩阵

syms 命令用来建立多个符号变量,一般调用格式为:

syms 符号变量1符号变量2 符号变量n

syms a b c; 等价

>> a=sym(‘a’);

>> b=sym(‘b’);

>> c=sym(‘c’);

建立符号表达式通常有以下2种方法:

(1)sym 函数直接建立符号表达式。
(2) 使用已经定义的符号变量组成符号表达式。

例如: y=sym(‘sin(x)+cos(x)’)

>> x=sym(‘x’);

>> y=sin(x)+cos(x)

>> syms x;

>> y=sin(x)+cos(x)

​ 符号表达式的替换

用给定的数据替换符号表达式中的指定的符号变量

subs(f,x,a)

a替换字符函数f 中的字符变量 x
a是可以是
数数值变量表达式 字符变量表达式

若x是一个由多个字符变量组成的数组或矩阵,

a 应该具有与 x相同的形状的数组或矩阵。

>> f= sym (‘ 2*u ‘);

>> subs(f,’ u ‘, 2 )

>> f2=subs(f,’ u ‘,’ u+2 ‘)

>> a=3;

>> subs(f2,’ u ‘, a+2 )

>> subs(f2,’ u ‘,’ a+2 ‘)

>> syms x y

>> f3=subs(f,’ u ‘, x+y )

>> subs(f3, [ x,y ] , [1,2] )

符号矩阵

使用 sym 函数直接生成

>> A= sym ( ‘ [1+x, sin(x); 5, exp(x)] ‘ )

将数值矩阵转化成符号矩阵

>> B= [2/3, sqrt(2); 5.2, log(3)];

>> C= sym (B)

符号矩阵中元素的引用和修改

>> A= sym ( ‘ [1+x, sin(x); 5, exp(x)] ‘ );

>> A(1,2) % 引用

​ 因式分解

factor() 也可用于正整数的分解

>> factor ( sym (‘12345678901234567890’)) l 大整数的分解要转化成符号常量

​ 函数展开

expand (f)

l多项式展开

>> syms x; f= (x+1)^6;

>> expand (f)

三角函数展开

>> syms x y; f= sin( x+y );

>> expand (f)

​ 合并同类项

**collect ( f, v\ ): 按指定变量 *v* 进行合并

collect (f): 按 默认变量 进行合并

syms x y;

>> f= x^2*y + y*x - x^2 + 2*x ;

>> collect (f)

>> collect ( f,y )

​ 函数简化

y=simple (f): f 尝试多种不同的算法进行简化,返回其中最简短的形式

[ How,y ]=simple (f): y f 的最简短形式, How 中记录的为简化过程中使用的方法。

f R HOW
2*cos(x)^2-sin(x)^2 3*cos(x)^2-1 simplify
(x+1)x(x-1) x^3-x combine(trig)
x^3+3x^2+3x+1 (x+1)^3 factor
cos(3*acos(x)) 4x^3-3x expand

y=simplify (f): *f* 进行简化

>> syms x; f= sin(x)^2 + cos(x)^2 ;

>> simplify (f)

>> syms c alpha beta;

>> f= exp(c*log(sqrt( alpha+beta )));

>> simplify (f)

例:简化 img

>> syms x;

>> f= (1/x^3+6/x^2+12/x+8)^(1/3);

>> y1=simplify (f)

g1=simple (f)

>> g2=simple (g1)

l 多次使用 simple 可以达到最简表达。

​ 分式通分

[N,D]= numden (f):

N 为通分后的分子, D 为通分后的分母

>> syms x y;

>> f= x/ y+y /x;

>> [N,D]= numden (f)

>> [ n,d ]= numden ( sym (112/1024))

​ horner 多项式

horner 多项式:嵌套形式的多项式

列:img

>> syms x;

>> f= x^4+2*x^3+4*x^2+x+1;

>> g= horner (f)

​ 计算导数

g=diff( f,v ) : 求符号表达式 f 关于 v 的导数

g=diff(f) : 求符号表达式 f 关于 默认变量 的导数

g=diff( f,v,n ) : 求 f 关于 v n 阶导数

g=diff(f,n):求f关于默认变量的n阶导数

>> syms x;

>> f= sin(x)+3*x^2 ;

>> g=diff ( f,x )

​ 计算积分

int( f,v,a,b ) : 计算定积分 img

int( f,a,b ) : 计算关于 默认变量 的定积分

int( f,v ) : 计算不定积分img

int( f ) : 计算关于 默认变量 的不定积分

例:计算 imgimg

>> syms x; f= (x^2+1)/(x^2-2*x+2)^2;

>> I=int ( f,x )

>> K=int (exp(-x^2),x,0,inf)

​ 符号求和

symsum ( f,v,a,b ) : 求和 img

symsum ( f,a,b ) : 关于 默认变量 求和
例:计算级数 img及其前100项的部分和

>> syms n; f= 1/n^2;

>> S= symsum (f,n,1,inf)

>> S100= symsum (f,n,1,100)

微分方程求解

dsolve

y= dsolve (‘ eq1 ‘ , ‘ eq2 ‘ , … , ‘ cond1 ‘ , ‘ cond2 ‘ , … , ‘ v ‘)

其中 y 为输出的解, eq1 、 eq2 、 . . . 为微分方程,
cond1 、 cond2 、 … 为初值条件, v 为自变量

例 1: 求微分方程img的通解,并验证。

>> y= dsolve ( ‘ Dy+2*x*y=x*exp(-x^2) ‘,’ x ‘)

>> y= C2*exp(-x^2) + (x^2*exp(-x^2))/2

例2:求微分方程img满足初值条件
img的特解,并画出解函数的图形

>> *y= dsolve (‘ x\ Dy+y-exp (x)=0 ‘, …
‘ y(1)=2*exp(1) ‘, ‘ x ‘)

>> ezplot (y);

例3: 求微分方程组img 在初值条件 img

下的特解,并画出解函数的图形。

**[ x,y ]= dsolve (‘ Dx+5\ x+y =exp(t) ‘,’ Dy-x-3*y=0 ‘, …*

‘ x(0)=1’, ‘y(0)=0 ‘, ‘ t ‘)

ezplot ( x,y ,[0,1.3]);

  •     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
            <use xlink:href="#csdnc-thumbsup"></use>
        </svg><span class="name">点赞</span>
        <span class="count">9</span>
        </a></li>
        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-csdnc-Collection-G"></use>
        </svg><span class="name">收藏</span></a></li>
        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-csdnc-fenxiang"></use>
        </svg>分享</a></li>
        <!--打赏开始-->
                                <!--打赏结束-->
                                <li class="tool-item tool-more">
            <a>
            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
            </a>
            <ul class="more-box">
                <li class="item"><a class="article-report">文章举报</a></li>
            </ul>
        </li>
                            </ul>
    </div>
                </div>
    <div class="person-messagebox">
        <div class="left-message"><a href="https://blog.csdn.net/qq_36666756">
            <img src="https://profile.csdnimg.cn/4/7/A/3_qq_36666756" class="avatar_pic" username="qq_36666756">
                                    <img src="https://g.csdnimg.cn/static/user-reg-year/2x/3.png" class="user-years">
                            </a></div>
        <div class="middle-message">
                                <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_36666756" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">ItsL</a></span>
                                    </div>
            <div class="text"><span>发布了33 篇原创文章</span> · <span>获赞 53</span> · <span>访问量 4万+</span></div>
        </div>
                        <div class="right-message">
                                    <a href="https://im.csdn.net/im/main.html?userName=qq_36666756" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                </a>
                                                    <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                            </div>
                    </div>
            </div>
    </article>
    

一、线性方程和非线性方程在MATLAB中的各种求解方法

1.求多项式方程的根

​ roots(p)

​ solve(p)

例:求方程image-20200130215910004的所有根

​ p=[1 -4 9 -10]

​ r=roots(p)

或 s1=str2sym(‘x^3-4*x^2+9*x-10’)

​ solve(s1)

2.求超越方程的根

二、MATLAB中求和及求极值的方法

1.求和

(1)向量或矩阵求和

(2)级数求和

2.求函数的极值点