加载中…
个人资料
石头
石头
  • 博客等级:
  • 博客积分:0
  • 博客访问:60,780
  • 关注人气:5
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
正文 字体大小:

[转载]MATLAB基础教程\实例原文件 部分

(2017-03-15 16:13:31)
标签:

转载

>> str=['MATLAB 2006a']

>> str

>> disp(str)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>> x=-2:0.2:2;

>> y=x.^2;

>> plot(x,y)

>> str1 = num2str(min(x));

>> str2 = num2str(max(x));

>> out = ['Value of f from ' str1 ' to ' str2];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> A(1,1) = {[1 4 3; 0 5 8; 7 2 9]};

>> A(1,2) = {'Anne Smith'};

>> A(2,1) = {3+7i};

>> A(2,2) = {-pi:pi/4:pi}

>> B=cell(2,2)

>> B(1,1)=A(1,1)
>> celldisp(A)
>> A{1,1}
>> cellplot(A)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> clear
A=cell(3,3);
for i=1:3
    for j=1:3
        A{i,j}=randn(i,j);
    end
end
>> subplot(2,1,1)
>> cellplot(A)
>> A(2)=[];
>> subplot(2,1,2)
>> cellplot(A)
>> clear
A=cell(3,3);
for i=1:3
    for j=1:3
        A{i,j}=randn(i,j);
    end
end
>> subplot(3,1,1)
cellplot(A)
A(2)=[];
subplot(3,1,2)
cellplot(A)
>> A(3:5)=[];
>> subplot(3,1,3)
cellplot(A)

[转载]MATLAB基础教程\实例原文件 <wbr>部分

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

>> person.name='liuhuiying';
>> person.height=162;
>> person.weight=51;
>> person.hobby='swimming';
>> person

>> person(2).name='zhangqiang';
>> person(2).height=175;
>> person(2).weight=65;
>> person(2).hobby='Game';
>> person

>> person(1)

>> person(2)

person =

      name: 'liuhuiying'
    height: 162
    weight: 51
     hobby: 'swimming'


person =

1x2 struct array with fields:
    name
    height
    weight
    hobby


ans =

      name: 'liuhuiying'
    height: 162
    weight: 51
     hobby: 'swimming'


ans =

      name: 'zhangqiang'
    height: 175
    weight: 65
     hobby: 'Game'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>> person=struct('name','liuhuiying','height',162,'weight',51,'hobby','swimming')

person =

      name: 'liuhuiying'
    height: 162
    weight: 51
     hobby: 'swimming'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>> person=struct('name',{'liuhuiying','zhangqiang'},'height',{162,175},'weight',{51,65},'hobby',{'swimming','Game'})

>> person(1)

>> person(2)

 


person =

1x2 struct array with fields:
    name
    height
    weight
    hobby


ans =

      name: 'liuhuiying'
    height: 162
    weight: 51
     hobby: 'swimming'


ans =

      name: 'zhangqiang'
    height: 175
    weight: 65
     hobby: 'Game'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> person(1).gender='female

>>person(1)

>>person(2)

person =

1x2 struct array with fields:
    name
    height
    weight
    hobby
    gender


ans =

      name: 'liuhuiying'
    height: 162
    weight: 51
     hobby: 'swimming'
    gender: 'female'


ans =

      name: 'zhangqiang'
    height: 175
    weight: 65
     hobby: 'Game'
    gender: []

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>> person=rmfield(person,'hobby')

>> person(1)

>> person(2)

 

person =

1x2 struct array with fields:
    name
    height
    weight
    gender


ans =

      name: 'liuhuiying'
    height: 162
    weight: 51
    gender: 'female'


ans =

      name: 'zhangqiang'
    height: 175
    weight: 65
    gender: []

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> person(2).gender

>> person(2).gender='male';

>> person(2)

 

ans =

     []


ans =

      name: 'zhangqiang'
    height: 175
    weight: 65
    gender: 'male'

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> A=[10 20 30 40 50]

>> B=[10;20;30;40;50]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> A=1:2:8

>> A=linspace(0,12,4)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> find((A>8)&(A<14))       %找出矩阵A中大于8并小于14的元素的单下标索引

>> A(find((A>8)&(A<14)))    %找出矩阵A中大于8并小于14的元素

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> A([1,3,6,8])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> A=rand(3,3)

>> A(:,:,2)=randn(3,3)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> A = rand(5);

>> B = A > 0.5;

>> A(B) = 0

 

A =

    0.1450    0.4018    0.2400    0.4909    0.1112
           0.0760    0.4173    0.4893         0
           0.2399    0.0497    0.3377    0.3897
    0.3510    0.1233                  0.2417
           0.1839           0.3692    0.4039

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

 >> A = magic(4)

>> B = isprime(A)

>> A(~B) = 0

 

%%%%%%%%A =

    16          13
       11    10     8
             12
       14    15     1


B =

              1
              0
              0
              0


A =

             13
       11        0
              0
              0

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

 >> r=[1 3 5];

>> p=poly(r)

>> poly2sym(p)

>> roots(p)

p =

       -9    23   -15

 
ans =
 
x^3 - 9*x^2 + 23*x - 15
 

ans =

    5.0000
    3.0000
    1.0000

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> p=[4 3 2 1];

>> polyval(p,4)

>> 4*4^3+3*4^2+2*4+1


ans =

   313


ans =

   313

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

% fit sine between 0 and 2*pi using 3 order polyn0mial
x = 0:pi/10:2*pi;
y = sin(x);
p = polyfit(x,y,3);
plot(x,y,'r*');
hold on
f = poly2sym(p);
ezplot(f,[0,2*pi]);

>> p

[转载]MATLAB基础教程\实例原文件 <wbr>部分

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

% Interpolation using the four methods
x=[0 3 5 7 9 11 12 13 14 15];
y=[0 1.2 1.7 2.0 2.1 2.0 1.8 1.2 1.4 1.6];
length_of_x=length(x);
scalar_x=[x(1):0.5:x(length_of_x)];
length_of_sx=length(scalar_x);
for i=1:length_of_sx
    y_nearest(i)=interp1(x,y,scalar_x(i),'nearest');
    y_linear(i) =interp1(x,y,scalar_x(i),'linear');
    y_spline(i) =interp1(x,y,scalar_x(i),'spline');
    y_cubic(i)  =interp1(x,y,scalar_x(i),'cubic');
end
subplot(2,2,1),plot(x,y,'*'),hold on,plot(scalar_x,y_nearest),title('method=nearest');
subplot(2,2,2),plot(x,y,'*'),hold on,plot(scalar_x,y_linear),title('method=linear');
subplot(2,2,3),plot(x,y,'*'),hold on,plot(scalar_x,y_spline),title('method=spline');
subplot(2,2,4),plot(x,y,'*'),hold on,plot(scalar_x,y_cubic),title('method=cubic');

[转载]MATLAB基础教程\实例原文件 <wbr>部分

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

>> fh = @(x,y)y*sin(x)+x*cos(y);

>> fh(pi,2*pi)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

 fh = @humps;

subplot(1,2,1),fplot(fh,[-3,3]);

subplot(1,2,2),fplot(fh,[-3,3,-10,30]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

 

% find the minimum of the function f(x)=x^3+a*x^2+b*x+c
function [x0,y] = funmin(a,b,c,x1,x2)
options = optimset('Display','off');
[x0,y] = fminbnd(@poly3,x1,x2,options);
    function y=poly3(x)        %the nested function
        y=x^3+a*x^2+b*x+c;
    end
% plot the function
fplot(@poly3,[x1,x2]);
hold on;
plot(x0,y,'.');
end

>> [x,y]=funmin(-1000,10,0,600,800)

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>> F = @(x,y)y*sin(x)+x*cos(y);

>> Q = dblquad(F,pi,2*pi,0,pi)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>> h_g=@(x,sigma2)exp(-x^2./(2.*(1:sigma2)))./sqrt(2*pi.*(1:sigma2));

>> quadv(@(x)h_g(x,3),0,2)

ans =

    0.4772    0.4214    0.3759

>> quadv(@(x)h_g(x,2),0,2)

ans =

    0.4772    0.4214

>> quadv(@(x)h_g(x,1),0,2)

ans =

    0.4772

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% the file to creat a function with parameters
function y = poly3_fun(x,a,b,c)
y = x^3+a*x^2+b*x+c;


% find the minimum of the function f(x)=x^3+a*x^2+b*x+c
function [x0,y] = funmin_para(a,b,c,x1,x2)
options = optimset('Display','off');
[x0,y] = fminbnd(@(x)poly3_fun(x,a,b,c),x1,x2,options);
% plot the function
end


>> [x,y]=funmin_para(-1000,10,0,600,800)

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

% polynomial operation
p1=[1 2 1];                         %定义多项式
p2=[1 1];
length_of_p1=length(p1);
length_of_p2=length(p2);
if length_of_p1 == length_of_p2     %判断两个多项式长度是否相等
    p1_plus_p2 =p1+p2;              %多项式相加
    p1_minus_p2=p1-p2;              %多项式相减
elseif length_of_p1 < length_of_p2
        temp_p1=[zeros(length_of_p2-length_of_p1) p1];
        p1_plus_p2 =temp_p1+p2;
        p1_minus_p2=temp_p1-p2;
else
    temp_p2=[zeros(length_of_p1-length_of_p2) p2];
    p1_plus_p2 =p1+temp_p2;
    p1_minus_p2=p1-temp_p2;
end
p1_multiply_p2=conv(p1,p2);         %多项式相乘
p1_divide_p2  =deconv(p1,p2);       %多项式除法
p1=poly2sym(p1)                     %显示多项式 p1
p1=poly2sym(p2)                     %显示多项式 p2
p1_plus_p2 =poly2sym(p1_plus_p2)
p1_minus_p2=poly2sym(p1_minus_p2)
p1_multiply_p2=poly2sym(p1_multiply_p2)
p1_divide_p2  =poly2sym(p1_divide_p2)

 

 

 

p1 =
 
x^2 + 2*x + 1
 
 
p1 =
 
x + 1
 
 
p1_plus_p2 =
 
x^2 + 3*x + 2
 
 
p1_minus_p2 =
 
x^2 + x
 
 
p1_multiply_p2 =
 
x^3 + 3*x^2 + 3*x + 1
 
 
p1_divide_p2 =
 
x + 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

2.编写一个脚本,判断输入字符串中每个单词的首字母是否为大写,若不是则将其修改为大写,其他字母为小写

参考答案:

脚本文件内容为:

str = 'this is the string to be converted';

nlength = length(str);

for k=1:nlength

    if (k==1 || isspace(str(k-1))) && (str(k)<='z' && str(k)>='a')

        str(k) = char(double(str(k)) - 32);

    end

end

disp(str);

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

4.创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生成绩进行管理,如计算总分、平均分、排列名次等。

参考答案:

>> structtest=struct('name',{},'number',{},'scores',{})

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

1.创建符号表达式

参考答案:

>> f = sym('sin(x)+x');

2.计算习题 1 中表达式在 处的值

>> subs(f,x,0)

ans =

     0

>> subs(f,x,pi/4)

ans =

    1.4925

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

xth = 0;
tic
for x = -10:0.5:10
    xth = xth + 1;
    if x <0
        y(xth) = -x^3 + 5;
    else
        y(xth) = x^3 + 5;
    end
end
toc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tic
x = -10:0.5:10;
y = zeros(1,length(x));
x0 = find(x>0,1,'first');
y(1:x0-1) = -x(1:x0-1).^3 + 5;
y(x0:end) = x(x0:end).^3 + 5;
toc

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

1. 在同一窗口中绘制函数在 之间的正弦曲线和余弦曲线,步长为 ,线宽为 4 个象素,正弦曲线设置为蓝色实线,余弦曲线颜色设置为红色虚线,两条曲线交点处,用红色星号标记

参考答案:

>> x=0:pi/10:2*pi;

>> sinx = sin(x);

>> cosx = cos(x);

>> figure,plot(x,sinx,'LineWidth',4)

>> hold on,plot(x,cosx,'r:','LineWidth',4)

>> hold on,plot(x(find(cosx==sinx)),cosx(find(cosx==sinx)),'r*','LineWidth',4)

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

1. 编写 MATLAB 程序,绘制下面的函数:

,其中

该程序在绘制图形之后等待用户的鼠标输入,每单击其中一条曲线,就随机修改该曲线的颜色,包括红色、绿色、蓝色、黑色和黄色。

参考答案:

t = -2:0.1:2;

x = cos(t/pi);

y = 2*sin(t/(2*pi));

figure,plot(t,x);

hold on; plot(t,y);

%if waitforbuttonpress

while(1)

    if waitforbuttonpress==0

        get(gcf,'CurrentObject');

        c = ceil((rand) * 5);

        switch c

            case 1

                set(get(gcf,'CurrentObject'),'Color', 'r');

            case 2

                set(get(gcf,'CurrentObject'),'Color', 'g');

            case 3

                set(get(gcf,'CurrentObject'),'Color', 'b');

            case 4

                set(get(gcf,'CurrentObject'),'Color', 'k');

            case 5

                set(get(gcf,'CurrentObject'),'Color', 'y');

        end

    end

end

[转载]MATLAB基础教程\实例原文件 <wbr>部分

0

  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有