matlab画立体仓库库位图_Matlab 画向量图和平面图

69c35920230e75b64ada2b539f557b51.png

使用matlab里面的quiver函数画单个的向量图和使用surf画平面图十分不方便,所以自己写了3个函数,以便快捷的画简单的向量图和平面图。

  1. 画向量图函数
function plotv3(v_start, v_end, v_size, v_color)
% This function is used to plot 3d vectors.
%
% Parameters:
%   v_start: the start point of a vector
%   v_end: the end point of a vector
%   v_size: the vector line width, optional
%   v_color: the vector's color, optional
%
% Examples:
%   plotv3([0,0,0],[1,1,1]), plot a vector
%   plotv3(p1, p2, 2), plot a vector,line width is 2
%   plotv3(p1, p2, 2, 'g'), plot a vector, line width is 2, color is green
%
% @Author: ERYUESANHI 
% @Date: 2020-11-03 20:25:48 

    v_style = quiver3(v_start(1),v_start(2),v_start(3), ...
                      v_end(1), v_end(2), v_end(3));
    v_style.MaxHeadSize = 0.3;
    v_style.LineWidth = 1.5;
    v_style.AutoScale = 'off';
    if nargin > 2
       v_style.LineWidth = v_size;
    end
    if nargin >3
        v_style.Color = v_color;
    end
    if nargin > 4 
        fprintf("Most 4 inputsn");
    end
    
end

66990b283c704abc5e329ac69faa6c9a.png

2. 画3xn矩阵的列向量图

function plotmv3(m)
% This function is used to plot 3d column vectors of a 3xn matrix.
%
% Parameters:
%   m: a 3xn matrix.
%
% Examples:
%   plotmv3(m);
%
% @Author: ERYUESANHI
% @Date: 2020-11-03 21:25:28 

    [row, col] = size(m);
    
    if row ~= 3
       fprintf("error! not 3 dimention");
    end
    
    for i=1:col
        v_style = quiver3(0,0,0, m(1,i), m(2,i), m(3,i));
        v_style.MaxHeadSize = 0.3;
        v_style.LineWidth = 1.5;
        hold on;
    end  
end

8b9980be026513898e6db9f945e6de09.png
正交矩阵的列向量

3. 画两个向量确定的平面

function plotvp(v1, v2, p_color, p_alpha, p_edge_color)
% This function is used to plot a plane formed by to vectors.
%
% Parameter:
%   v1: vector 1
%   v2: vector 2
%   p_color: plane color, optional
%   p_alpha: plane transparency, optional
%   p_edge_color: plane grid color, optional
%
% Examples:
%   plotvp([1,2,3],[2,3,4]), plot a plane formed by [1,2,3]' and [2,3,4];
%   plotvp(v1,v2,'r'), the plane is red;
%   plotvp(v1,v2,'r','0.5'), the red plane's alpha is 0.5;
%   plotvp(v1,v2,'r','0.5','g'), the plane with green grids.
%
% @Author: ERYUESANHI
% @Date: 2020-11-03 20:25:48 

    syms x y z;
    n = cross(v1,v2);
    if n(3) ~= 0
        z = (-n(1)*x - n(2)*y)/n(3);
    else
        z = 0;
    end
    fs = fsurf(x,y,z);
    fs.FaceColor = '#cdcdcd';
    fs.FaceAlpha = 0.5;
    fs.EdgeColor = 'none';
    
    if nargin > 2
        fs.FaceColor = p_color;
    end
    if nargin > 3
        fs.FaceAlpha = p_alpha;
    end
    if nargin > 4
        fs.EdgeColor = p_edge_color;
    end
    if nargin > 5
        fprintf("Most 5 inputsn");
    end
end

e7e023d3876934636e71bdcb273f8904.png

一个综合示例:

clear; clc; close all;

O = [0,0,0];

e1 = [1,0,0]';
e2 = [0,1,0]';
e3 = [0,0,1]';

plotvp(e1,e2,'g',0.3);  % 画e1,e2张成的平面
hold on
plotvp((e1+e2+e3),[2,4,0],'r',0.3) % 画e1+e2+e3和[2 4 0]'所张成的平面
plotv3(O,[2 4 0]) % 画[2 4 0]向量
plotmv3([e1,e2,e3]) % 画三个基向量
plotv3(O,(e1+e2+e3)) % 三个基向量的和向量
plotv3(O,(e1+e2)) % e1和e2的和向量, e1+e2+e3在e1,e2所在平面的投影
plotv3((e1+e2),[1 1 1]-[e1+e2]) % e1+e2的正交向量
axis([-1.5,1.5,-1.5,1.5,-0.5,3])

209f6ed5b9087eb7294023c6e8027670.png

9b5f53d191cd5e910e8336677793b1d6.png
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页