zoukankan      html  css  js  c++  java
  • matlab基础向7-8:画图

    1.画直角坐标系的二维图

    画直线:

    x1=[1 2 3];
    y1=[4 5 6];
    plot(x1,y1);%斜率为1的直线,穿过(1,4)(2,5)(3,6)
    

    画抛物线y=x*x(-3<=x<=3):

    x2=-3:0.1:3;%每隔0.1就有一个点
    y2=x2.*x2; %x2.^2
    plot(x2,y2,'green-o');%关键点有一个圈表示
    plot(x2,y2,'red-.');%虚线
    axis equal %设置横坐标和纵坐标长度相同

    plot函数是从左到右把点连起来,点越多,越光滑,看起来就像曲线了。

     

    2.画直方统计图(bar图)

    %直方统计图,bar图
    y3=[ 12 23 42 15 10 135 16 17 ];
    bar(y3);%横坐标默认从1开始,每次间隔1
    x3=2001:1:2008
    bar(x3,y3);%横坐标自己定义
    

     

    3.三维图形

    %三维图像plot3:画一个圆绕z轴螺旋上升
    t=0:pi/50:6*pi %t为三角函数角度,pi是圆周率
    x4=cos(t);
    y4=sin(t);
    plot(x4,y4);%底面的圆
    z=0:1:300;%301个点,和x4y4对应
    plot3(x4,y4,z);%画出三维图

    4.各种标识功能

    每次使用plot函数都会覆盖之前的图像,想要保留之前的图像,在上一次plot函数后用语句“hold on;”。

    加背景网格:grid on;

    加标题:title('标题内容');

    横纵坐标加标签:xlabel('横坐标标签');ylabel('纵坐标标签');

    x1=[1 2 3];
    y1=[4 5 6];
    plot(x1,y1);%斜率为1的直线,穿过(0,3)
    hold on;%保留
    x2=-3:0.1:3;
    y2=x2.*x2; %x2.^2
    plot(x2,y2,'green-o');%关键点有一个圈表示
    axis equal %横坐标和纵坐标长度相同
    
    grid on;
    title('两个图');
    xlabel('x坐标');
    ylabel('y坐标');
    

    5.划分功能(把窗口分割画多个图)

    subplot(行,列,第几个);%第几个是从左往右数,换行

    x=-4:0.1:4
    y1=sin(x);
    y2=sin(2.*x);
    y3=sin(3.*x);
    y4=sin(4.*x);
    
    subplot(2,2,1);
    plot(x,y1);
    title('y=sin x');
    
    subplot(2,2,2);
    plot(x,y2);
    title('y=sin 2x');
    
    subplot(2,2,3);
    plot(x,y3);
    title('y=sin 3x');
    
    subplot(2,2,4);
    plot(x,y4);
    title('y=sin 4x');
    

    x=-4:0.1:4
    y1=cos(x);
    y2=cos(2.*x);
    y3=cos(3.*x);
    
    subplot(2,2,1);
    plot(x,y1);
    title('y=cos x');
    
    subplot(2,2,2);
    plot(x,y2);
    title('y=cos 2x');
    
    subplot(2,2,[3,4]);%合并
    plot(x,y3);
    title('y=cos 3x');
    

    6.画曲面

    例如:z=x^2 + y^2

    第一步,确定x和y的范围

    x=-3:1:3;

    y=-3:1:3;

    第二步,确定z范围

    z=x.^2 + y.^2;

    可以看到

    x = -3 -2 -1 0 1 2 3

    y = -3 -2 -1 0 1 2 3

    z = 18 8 2 0 2 8 18

    显然7个点怎么可能形成一个曲面?脑补一下这7个点形成一条曲线,投影在平面上是y=x(-3<=x<=3)这条直线。

    第三步,想办法找出其他关键点

    [X,Y]=meshgrid(x,y);

    Z=X.^2 + Y.^2;

    可以看到

    X =

    -3 -2 -1 0 1 2 3
    -3 -2 -1 0 1 2 3
    -3 -2 -1 0 1 2 3
    -3 -2 -1 0 1 2 3
    -3 -2 -1 0 1 2 3
    -3 -2 -1 0 1 2 3
    -3 -2 -1 0 1 2 3

    Y =

    -3 -3 -3 -3 -3 -3 -3
    -2 -2 -2 -2 -2 -2 -2
    -1 -1 -1 -1 -1 -1 -1
    0 0 0 0 0 0 0
    1 1 1 1 1 1 1
    2 2 2 2 2 2 2
    3 3 3 3 3 3 3

    (meshgrid通过一维数组生成二维矩阵,其中矩阵X的行向量是向量x的简单复制,而矩阵Y的列向量是向量y的简单复制)

    第四步,用surf函数生成曲面

    surf(X,Y,Z);

  • 相关阅读:
    C# int类型的GetProperty的PropertyType返回的是Int32,无法通过typeof(int)或者typeof(Int32)进行比较
    MVC5 Authentication身份认证
    java各个类型判断为空
    table固定列宽,每列超出部分用...代替,鼠标悬停显示全部内容
    web前后台数据交互的四种方式(转)
    List<Map<String,String>>操作(遍历,比较)
    JavaScript实现简单的打印功能
    Expected one result (or null) to be returned by selectOne(), but found: 2
    Eclipse Code Template 设置自动加注释(转)
    highcharts动态获取数据生成图表问题
  • 原文地址:https://www.cnblogs.com/shoulinniao/p/11192541.html
Copyright © 2011-2022 走看看