全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 数据分析与数据挖掘
1106 1
2016-03-09

MATLAB课程:代码示例之Programming(五)


Function Functions


This example shows how to use the output of one MATLAB® function as an input to another. This ability to specify a function's output as an input of another function serves a wide variety of purposes. Here we illustrate its use for finding zeros, optimization, and integration.


The HUMPS Function

A MATLAB function is a file that starts with the keyword function. This is what the function HUMPS looks like:

type humps


function [out1,out2] = humps(x)%HUMPS  A function used by QUADDEMO, ZERODEMO and FPLOTDEMO.%   Y = HUMPS(X) is a function with strong maxima near x = .3%   and x = .9.%%   [X,Y] = HUMPS(X) also returns X.  With no input arguments,%   HUMPS uses X = 0:.05:1.%%   Example:%      plot(humps)%%   See QUADDEMO, ZERODEMO and FPLOTDEMO.%   Copyright 1984-2014 The MathWorks, Inc.if nargin==0   x = 0:.05:1;endy = 1 ./ ((x-.3).^2 + .01) + 1 ./ ((x-.9).^2 + .04) - 6;if nargout==2,   out1 = x;   out2 = y;else   out1 = y;end


Plot of HUMPS

This figure shows a plot of HUMPS in the domain [0,2] using FPLOT.

fplot(@humps,[0,2]);


Zero of HUMPS

The FZERO function finds a zero of a function near an initial estimate. Our initial guess for the zero of HUMPS is 1.

z = fzero(@humps,1,optimset('Display','off'));fplot(@humps,[0,2]);hold on;plot(z,0,'r*');hold off


Minimum of HUMPS

The FMINBND function finds the minimum of a function in a given domain. Here, we search for a minimum for HUMPS in the domain (0.25, 1).

m = fminbnd(@humps,0.25,1,optimset('Display','off'));fplot(@humps,[0 2]);hold on;plot(m,humps(m),'r*');hold off


Integral of HUMPS

The INTERGRAL function finds the definite integral of HUMPS in a given domain. Here it computes the area in the domain [0.5, 1].

q = integral(@humps,0.5,1);fplot(@humps,[0,2]);title(['Area = ',num2str(q)]);




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-3-9 14:53:30
挺有用的,受教了。。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群