发布网友 发布时间:2022-04-22 21:46
共5个回答
热心网友 时间:2023-06-21 11:33
问题描述:例如在命令窗口中 function热心网友 时间:2023-06-21 11:33
m文件函数是Matlab中的子函数,其格式为:function[输出参数列表]=函数名(输入参数列表)函数体举个例子,编写一个求自变量X的正弦值的m函数,如下:function y=mysin(x)y=sin(x);% 函数体此时在Matlab命令窗口输入若下内容时>>x=pi/2;>>y=mysin(x)Matlab便会调用y.m文件子函数,计算sin值,并给出结果为>>y=1例子有点简单,自己琢磨编写更强大的函数吧,注学习快乐!你觉得有帮助,别忘了采纳(⊙o⊙)哦!]热心网友 时间:2023-06-21 11:34
用.m文件来定义就行了: 比如:function y=num(a,b); y=a+b; 定义好后,保存为mum_1.m文件 比如你要计算2+3;就直接在command window里面输入num_1(2+3)=就行了;]热心网友 时间:2023-06-21 11:34
M函数除了直接用函数名调用之外,也可以进行参数传递,使得Matlab应用更加方便。M函数文件以function开头,格式为function 输出变量 = 函数名称(输入变量)语句;例如:%eg_1f.mfunction s=f(m)s=0for n=1:ms=s+1/n/n;end保存为eg_1f.m,然后在指命窗口执行>>eg_1f(100)ans = 1.6350]热心网友 时间:2023-06-21 11:35
输入:help function就会出现帮助! FUNCTION Add new function. New functions may be added to MATLAB's vocabulary if they are expressed in terms of other existing functions. The commands and functions that comprise the new function must be put in a file whose name defines the name of the new function, with a filename extension of '.m'. At the top of the file must be a line that contains the syntax definition for the new function. For example, the existence of a file on disk called STAT.M with: function [mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).^2)/n); defines a new function called STAT that calculates the mean and standard deviation of a vector. The variables within the body of the function are all local variables. See SCRIPT for proceres that work globally on the work- space. A subfunction that is visible to the other functions in the same file is created by defining a new function with the FUNCTION keyword after the body of the preceding function or subfunction. For example, avg is a subfunction within the file STAT.M: function [mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = avg(x,n); stdev = sqrt(sum((x-avg(x,n)).^2)/n); %------------------------- function mean = avg(x,n) %AVG subfunction mean = sum(x)/n; Subfunctions are not visible outside the file where they are defined. Normally functions return when the end of the function is reached. A RETURN statement can be used to force an early return. See also script, return, varargin, varargout, nargin, nargout, inputname, mfilename. Reference page in Help browser doc function]