Dont understand the code

1 view (last 30 days)
siyuan yang
siyuan yang on 28 Jan 2015
Commented: Star Strider on 28 Jan 2015
my code is:
*function [ y ] = integral(n0)
y=0;
for n=n0:-1:20
y=1/10*(1/n-y);
end
z = quad(@(x)fun1_6(x ,20) ,0 ,1 ,1.e-10);
fprintf('%e %e %e\n',y,z,abs(y-z))*
I dont understand the line "z = quad(@(x)fun1_6(x ,20) ,0 ,1 ,1.e-10)" can any one explain to me?

Answers (1)

Star Strider
Star Strider on 28 Jan 2015
Edited: Star Strider on 28 Jan 2015
That line integrates ‘fun1_6’, that takes two arguments, x and second argument that is assigned the value of 20 here, and using the quad function, numerically integrates it over x only, from 0 to 1, with a tolerance of 1e-10, and assigns it to the variable z.
  2 Comments
siyuan yang
siyuan yang on 28 Jan 2015
what is @(x) means?
Star Strider
Star Strider on 28 Jan 2015
The ‘@(x)’ is a function handle, in this instance to an anonymous function created so that ‘fun1_6’ is only passed a value for x.
In the function handle documentation (that I linked to here), see specifically Example 2 — Constructing a Handle to an Anonymous Function.
The documentation explains it much better than I can.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!