首页/文章/ 详情

Matlab 自定义函数初讲

3年前浏览2080

image.png

 过冷水诚挚邀请你加入Matlab仿真秀官方交流群进行Matlab学习、问题咨询、 Matlab相关资料下载,群号:927550334

wayne.gif

        在Matlab中使用函数可以把一个较大的任务分解为多个较小的任务,有时候为了程序简介不要重复性写代码就可以写成函数调用的形式,过冷水接触较多的就是在绘图过程中设置为各种形式参数可以写成函数形式。

untitled.jpg

        需要绘制上图的完整代码涉及各种样式的设置,代码很冗长;

clear 
S=dsolve('D2x-(4/2)*(y-x) (2/2)*x=0','D2y (4/5)*(y-x) (3/2)*y=0','x(0)=0','y(0)=5','Dx(0)=1','Dy(0)=2')
t=linspace(0,50,500);
x=cos(atan((689^(1/2)/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)) - 73/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)))/((2067*(689^(1/2)/20   53/20)^(1/2))/(16*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))) - (345*(689^(1/2)/20   53/20)^(3/2))/(8*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)))   (299*689^(1/2)*(689^(1/2)/20   53/20)^(1/2))/(16*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))) - (2645*689^(1/2)*(689^(1/2)/20   53/20)^(3/2))/(424*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))))) - t*(689^(1/2)/20   53/20)^(1/2))*((689^(1/2)/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)) - 73/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)))^2/((345*(689^(1/2)/20   53/20)^(3/2))/(8*689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 424*(689^(1/2)/20   53/20)^(1/2)   160*(689^(1/2)/20   53/20)^(3/2)) - (2067*(689^(1/2)/20   53/20)^(1/2))/(16*689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 848*(689^(1/2)/20   53/20)^(1/2)   320*(689^(1/2)/20   53/20)^(3/2)) - (299*689^(1/2)*(689^(1/2)/20   53/20)^(1/2))/(16*689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 848*(689^(1/2)/20   53/20)^(1/2)   320*(689^(1/2)/20   53/20)^(3/2))   (2645*689^(1/2)*(689^(1/2)/20   53/20)^(3/2))/(424*689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 22472*(689^(1/2)/20   53/20)^(1/2)   8480*(689^(1/2)/20   53/20)^(3/2)))^2   1)^(1/2)*((2067*(689^(1/2)/20   53/20)^(1/2))/(16*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))) - (345*(689^(1/2)/20   53/20)^(3/2))/(8*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)))   (299*689^(1/2)*(689^(1/2)/20   53/20)^(1/2))/(16*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))) - (2645*689^(1/2)*(689^(1/2)/20   53/20)^(3/2))/(424*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))))   cos(atan(((43*689^(1/2))/(26*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)))   63/(2*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))))/((3445*(53/20 - 689^(1/2)/20)^(1/2))/(16*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))) - (575*(53/20 - 689^(1/2)/20)^(3/2))/(8*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)))   (35*689^(1/2)*(53/20 - 689^(1/2)/20)^(1/2))/(16*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))) - (4025*689^(1/2)*(53/20 - 689^(1/2)/20)^(3/2))/(5512*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))))) - t*(53/20 - 689^(1/2)/20)^(1/2))*(((43*689^(1/2))/(26*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)))   63/(2*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))))^2/((575*(53/20 - 689^(1/2)/20)^(3/2))/(424*(53/20 - 689^(1/2)/20)^(1/2) - 80*(53/20 - 689^(1/2)/20)^(3/2)) - (3445*(53/20 - 689^(1/2)/20)^(1/2))/(848*(53/20 - 689^(1/2)/20)^(1/2) - 160*(53/20 - 689^(1/2)/20)^(3/2)) - (35*689^(1/2)*(53/20 - 689^(1/2)/20)^(1/2))/(848*(53/20 - 689^(1/2)/20)^(1/2) - 160*(53/20 - 689^(1/2)/20)^(3/2))   (4025*689^(1/2)*(53/20 - 689^(1/2)/20)^(3/2))/(292136*(53/20 - 689^(1/2)/20)^(1/2) - 55120*(53/20 - 689^(1/2)/20)^(3/2)))^2   1)^(1/2)*((3445*(53/20 - 689^(1/2)/20)^(1/2))/(16*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))) - (575*(53/20 - 689^(1/2)/20)^(3/2))/(8*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)))   (35*689^(1/2)*(53/20 - 689^(1/2)/20)^(1/2))/(16*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))) - (4025*689^(1/2)*(53/20 - 689^(1/2)/20)^(3/2))/(5512*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))));
y=cos(atan(((2*689^(1/2))/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)) - 30/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)))/((159*(689^(1/2)/20   53/20)^(1/2))/(2*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))) - (15*(689^(1/2)/20   53/20)^(3/2))/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))   (23*689^(1/2)*(689^(1/2)/20   53/20)^(1/2))/(2*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))) - (115*689^(1/2)*(689^(1/2)/20   53/20)^(3/2))/(53*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))))) - t*(689^(1/2)/20   53/20)^(1/2))*(((2*689^(1/2))/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)) - 30/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)))^2/((15*(689^(1/2)/20   53/20)^(3/2))/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2)) - (159*(689^(1/2)/20   53/20)^(1/2))/(2*689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 106*(689^(1/2)/20   53/20)^(1/2)   40*(689^(1/2)/20   53/20)^(3/2)) - (23*689^(1/2)*(689^(1/2)/20   53/20)^(1/2))/(2*689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 106*(689^(1/2)/20   53/20)^(1/2)   40*(689^(1/2)/20   53/20)^(3/2))   (115*689^(1/2)*(689^(1/2)/20   53/20)^(3/2))/(53*689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 2809*(689^(1/2)/20   53/20)^(1/2)   1060*(689^(1/2)/20   53/20)^(3/2)))^2   1)^(1/2)*((159*(689^(1/2)/20   53/20)^(1/2))/(2*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))) - (15*(689^(1/2)/20   53/20)^(3/2))/(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))   (23*689^(1/2)*(689^(1/2)/20   53/20)^(1/2))/(2*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))) - (115*689^(1/2)*(689^(1/2)/20   53/20)^(3/2))/(53*(689^(1/2)*(689^(1/2)/20   53/20)^(1/2) - 53*(689^(1/2)/20   53/20)^(1/2)   20*(689^(1/2)/20   53/20)^(3/2))))   cos(atan(((14*689^(1/2))/(13*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)))   34/(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)))/((265*(53/20 - 689^(1/2)/20)^(1/2))/(2*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))) - (25*(53/20 - 689^(1/2)/20)^(3/2))/(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))   (35*689^(1/2)*(53/20 - 689^(1/2)/20)^(1/2))/(26*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))) - (175*689^(1/2)*(53/20 - 689^(1/2)/20)^(3/2))/(689*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))))) - t*(53/20 - 689^(1/2)/20)^(1/2))*(((14*689^(1/2))/(13*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)))   34/(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)))^2/((25*(53/20 - 689^(1/2)/20)^(3/2))/(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2)) - (265*(53/20 - 689^(1/2)/20)^(1/2))/(106*(53/20 - 689^(1/2)/20)^(1/2) - 20*(53/20 - 689^(1/2)/20)^(3/2)) - (35*689^(1/2)*(53/20 - 689^(1/2)/20)^(1/2))/(1378*(53/20 - 689^(1/2)/20)^(1/2) - 260*(53/20 - 689^(1/2)/20)^(3/2))   (175*689^(1/2)*(53/20 - 689^(1/2)/20)^(3/2))/(36517*(53/20 - 689^(1/2)/20)^(1/2) - 6890*(53/20 - 689^(1/2)/20)^(3/2)))^2   1)^(1/2)*((265*(53/20 - 689^(1/2)/20)^(1/2))/(2*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))) - (25*(53/20 - 689^(1/2)/20)^(3/2))/(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))   (35*689^(1/2)*(53/20 - 689^(1/2)/20)^(1/2))/(26*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))) - (175*689^(1/2)*(53/20 - 689^(1/2)/20)^(3/2))/(689*(53*(53/20 - 689^(1/2)/20)^(1/2) - 10*(53/20 - 689^(1/2)/20)^(3/2))));
figure1 = figure;
subplot1 = subplot(1,2,1,'Parent',figure1);
hold(subplot1,'on');
plot(t,x,'Parent',subplot1,'LineWidth',3);
plot(t,y,'Parent',subplot1,'LineWidth',3,'MarkerSize',3);
ylabel(subplot1,'$x$','FontSize',20,'Interpreter','latex');
xlabel(subplot1,'$t$','FontSize',20,'Interpreter','latex');
hold(subplot1,'off');
set(subplot1,'FontName','Times New Roman','LineWidth',3);
title('运动轨迹','FontName','楷体')
legend(subplot1,'A','B')
subplot2= subplot(1,2,2,'Parent',figure1);
hold(subplot2,'on');
plot(t,x-y,'Parent',subplot2,'LineWidth',3);
ylabel(subplot2,'$\Delta x$','FontSize',20,'FontName','Times New Roman','Interpreter','latex');
xlabel(subplot2,'$t$','FontSize',20,'Interpreter','latex');
hold(subplot2,'off');
set(subplot2,'FontName','Times New Roman','LineWidth',3);
title('运动轨迹','FontName','楷体')
legend(subplot2,'A')

    没有必要每次画类似的图都把上述代码写一遍,写一次这样的程序,然后将其保存为函数形式,在下次使用的时候直接进行调用就可以了,非常省事。

function createfigure(X1, YMatrix1, Y1)
figure1 = figure;
subplot1 = subplot(1,2,1,'Parent',figure1);
hold(subplot1,'on');
plot1 = plot(X1,YMatrix1,'Parent',subplot1,'LineWidth',3);
set(plot1(1),'DisplayName','A');
set(plot1(2),'DisplayName','B','MarkerSize',3);
ylabel('$x$','FontSize',20,'FontName','Times New Roman',...
    'Interpreter','latex');
xlabel('$t$','FontSize',20,'FontName','Times New Roman',...
    'Interpreter','latex');
title('运动轨迹','FontName','楷体');
hold(subplot1,'off');
set(subplot1,'FontName','Times New Roman','LineWidth',3);
legend(subplot1,'show');
subplot2 = subplot(1,2,2,'Parent',figure1);
hold(subplot2,'on');
plot(X1,Y1,'DisplayName','A','Parent',subplot2,'LineWidth',3);
ylabel('$\Delta x$','FontSize',20,'FontName','Times New Roman',...
    'Interpreter','latex');
xlabel('$t$','FontSize',20,'FontName','Times New Roman',...
    'Interpreter','latex');
title('运动轨迹','FontName','楷体');
hold(subplot2,'off');
set(subplot2,'FontName','Times New Roman','LineWidth',3);
legend(subplot2,'show');

        以上就是将绘图样式设置成了子函数形式,直接调用就可以了,非常方便。本期过冷水就给大家讲讲关自定义函数的知识。

1.子函数:

(1)在一个M文件中,可以包含一个以上的函数,其中只有一个是主函数,其他的函数都是子函数。

(2)在一个M文件中,子函数必须出现在最上方,其后是子函数,子函数的次序没有任何限制。

(3)子函数不能被其他文件的函数调用,只能被同一文件中函数调用。

(4)同一文件的主函数和子函数变量工作空间相互独立。

    案例:编制一个函数,要求输入两个数值后,用第一个子函数求积,第二个子函数求和,再将这两个值求差的绝对值。

  • function f=jueduicha(x,y)

  • f=abs(ji(x,y)-he(x,y));

  • %子函数2

  • function f1=ji(x,y)

  • f1=x.*y;

  • %子函数2

  • function f2=he(x,y)

  • f2=x y;

>> f=jueduicha(2,3)

f =

     1

2.私有函数:私有函数是指放在private子目录中的M函数文件。

(1)在private目录下的私有函数,只能被其父目录中的M函数文件所调用,而不能和其他目录中的函数调用,对其他目录中额文件私有函数是不可见的,所以私有函数可以和其他目录中下的函数重名,不过过冷书不建议大家重名命名函数,过冷书的Big project 就深受其苦。

(2) 私有函数目录的M脚本文件也不可调用私有函数。

(3) 在函数搜索时,私有函数优于其他Matlab路径上的函数。

    以前在过冷水的推文中从来没有和大家讲过局部变量和全局变量这个概念,在引进函数后,就需要注意了。根据变量的作用域不同,可以将Matlab程序的变量分为局部变量和全局变量。

    局部变量:局部变量是在函数内部使用的变量,其影响范围只能在本函数内;每个函数运行时都占用独立的函数工作空间,此空间和Maltlab的工作空间是相互独立的,局部变量仅存在于函数的工作空间内。局部变量只在函数执行期间存在,当函数执行完毕时,局部变量就消失。

    全局变量:全局变量是可以在不同函数工作空间和Matlab工作空间中共享使用的变量。全局变量在使用时必须用global定义,而且每个共享全局变量的函数和工作空间都必须逐个用global对变量加以定义。

function u=quanju(t)

global X

X=1;

t=t;

u=quanju1(t);

function  u=quanju1(t)

global X

u=X-t;

>> u=quanju(3)

u =

    -2

function u=quanju(t)

global X

X=1;

t=t;

u=quanju1(t);

function  u=quanju1(t)

u=X-t;

u=quanju(3)

未定义函数或变量 'X'。

出错 quanju>quanju1 (line 8)

以上就是全局变量的使用方式,定义后需要再次定义才能识别。这在自写函数中很容易犯错。

    Matalb函数调用过程实际上就是参数传递的过程,传递方式为按值传递。

[输出参数1,输出参数2,....]=函数名[输入参数1,输入参数2,....]

1.参数传递规则

    在Matlab中函数具有自己的工作空间,函数内变量与外界的唯一联系就是通过函数的输入输出参数。输入参数在函数中的任何变化,都仅在函数内进行,不会传递回去。

function [u,v]=canshu(x,y,z)

u=x*y;

v=x.^z;

>> [u,v]=canshu(1,2,3)

u =

     2

v =

     1

2.函数参数的个数

    在Matalb中输入输出函数的个数是可以变化的,用户可以根据参数的个数来编程。

(1)nargin 和nargout 变量,

    调用格式:nargin:功能:在函数体内获取实际输入变量的个数。

    调用格式:nargout:功能:在函数体内获取实际输出变量的个数。

    调用格式:nargin(’fun’):功能:在函数体外获取实际输入变量的个数。

    调用格式:nargout(‘fun’):功能:在函数体外获取实际输出变量的个数。

function c=canshugeshu(a,b)

 if nargin ==1;

     c=a.^2;

 elseif nargin ==2;

     c=a b;

 end

>> c=canshugeshu(2,3)

c =

     5

>> c=canshugeshu(3)

c =

     9

    如果你能够做到灵活运用过冷水所讲的上述知识。那么恭喜你在Matalb学习应用中会更加得心应手,灵活运用自定义函数可以减少很多冗长的代码,让你的程序看起来更加的上档次。本期过冷水的讲解就这么多, 感兴趣的持续关注过冷水的相关文章。

图片

过冷水发表于仿真秀 平台原创文章,未经授权禁止私自转载,如需转载请需要和作者沟通表明授权声明,未授权文章皆视为侵权行为,必将追责。如果您希望加入Matlab仿真秀官方交流群进行Matlab学习、问题咨询、 Matlab相关资料下载均可加群:927550334。

精品回顾

matlab绘制农夫过河动态图

分子动力学的原子空间运动轨迹演示编程

过冷水带你用matlab制作演示动画

python批量移动文件&重命名代码分享

过冷水和你分享 matlab读取存储各种文件的方法 文末有独家金曲分享

image.png

理论科普代码&命令MATLAB
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2021-04-25
最近编辑:3年前
过冷水
博士 | 讲师 讨论号:927550334
获赞 357粉丝 178文章 109课程 11
点赞
收藏
作者推荐
¥5 5.0
未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈