Basic function in MATLAB -
i starting work matlab , still don't know how lot of things. have create simple plot function, let's supose is:
f(x)=3x+1, defined example -5 < x < 5
what need create function transforms independent variable (it's input parameter). example, if function named plotfunction
, if execute command plotfunction(2x+3)
final plotted graphic function:
f(x)=6x+10
functions can defined function
environment. want function has no output variable, plot apparently.
function [] = plotfunction(f,minrange,maxrange) range = minrange:0.01:maxrange; % create plot range, change 0.01 w/e precision want attain figure; % create figure plotfunc = 3.*f(range)+1; % function plot(range,plotfunc) end
there's 2 things need know when trying use other functions 'base function': have set take steps of 0.01
might need set smaller (e.g. when plotting on [-1e-4,1e-4]). can hand or use precision switch @efirvida used. other thing: call 3*f(range)+1
plotfunc
. if want use other functions, there, e.g. if want use cos(x)+1/3*sin(pi*x)*e^(-x))
set x
f(x)
: plotfunc = cos(f(x))+1/3.*sin(pi.*f(x))*exp(-f(x)))
now have take care call x
function handle
, so: f = @(x)(2*x+3)
. @
makes function handle, argument directly behind defines variable in defined function, here that's (x)
. second set of brackets contains actual function. define range, i.e. minrange = -5
maxrange = 5
, call function:
plotfunction(f,minrange,maxrange)
resulting in:
Comments
Post a Comment