Professional Documents
Culture Documents
FUNCTION
FUNCTION
FUNCTION
1. Example 3.4
%exf11.m
% This code is defines a function, ‘exf1’, that evaluates e^x.
% This function takes ‘x’ as an input argument and is called
% e^x = 1+x+x^2/2!+x^3/3!+x^4/4!+…
function ex = exf1(x)
term(1) = 1.0;
for n = 1:99
term(n+1) = term(n)*x/n;
break;
end
end
ex = sum(term);
Output
2. Example 3.5
% example_3_5.m
% to calculate w, y and z.
clear:clc;
fo = fopen (‘output.txt’,’w’);
fprintf (fo,’------------------\n’);
for x = 0.1:0.1:2.0
y = exf1(x);
w = 5*x*exf1(x);
z = 10*x/y;
end
fclose(fo) ;
output
3. Example 3.6
% Example_3_6.m
% function exf2.
% program.
% the function.
clear; clc;
z = 5;
for w = 0.2:0.2:1.0
q = exf2 (w,z);
end
% exf2.m
% program, but need not have the same names. The output
% program.
% terms.
% e^x = 1+x+x^2/2!+x^3/3!+x^4/4!+…
function exy = exf2 (x,y)
for n = 1:100
term = term*x/n;
s = s+term;
break;
end
end
exy = s/y;