Script AnonymF2.m
Download Script AnonymF2.m
%----------------------------------------------------------
%A script using an anonymous function
%with more arguments M-file:AnonymF2.m
%----------------------------------------------------------
%%
%polynomial of degree two is defined in the range (-40,40)
x=-40:0.5:40;
%coefficients of polynomial a, b, c
%are the arguments of the anonymous function
pol_degree2=@(a,b,c)(a*x.^2+b*x+c);
%a, b, c are input argoments of the function
%polinomial is calculated
y=pol_degree2(-2,3.5,2.75);
%y being a variable of class double can be used
%with PLOT function
plot(x,y),title('polinomial of degree two')
%----------------------------------------------------------
%%
%anonymous function uses a cell array
%for calculation of a polynomial of degree two
A={@(a1,x1)a1*x1.^2,@(b1,x1)b1*x1,@(c1)c1};
%values of a1, b1, c1 and x1
%are the input arguments
y1=A{1}(-2,[-5:1:5])+A{2}(3.5,[-5:1:5])+A{3}(2.75);
%but the PLOT function cannot be used
%since the x1 array is implicitly defined
%----------------------------------------------------------
%%
B={@(a2,x2)a2*x2.^2,@(b2,x2)b2*x2,@(c2)c2};
%now the PLOT function can be used
%the x2 array is explicitly defined
x2=-10:1:10;
%the returning argument y2 is calculated
y2=B{1}(-2,x2)+B{2}(3.5,x2)+B{3}(2.75);
%since arrays x2 and y2 are explicitly defined
%the PLOT function can be used
figure
plot(x2,y2),title('polinomial of degree two using cell')
whos
%----------------------------------------------------------
Back