user3415893
user3415893

Reputation: 69

Matlab. Difference between plot and fplot?

Why use 'fplot' to plot functions when we can use 'plot'? I can't understand

Upvotes: 3

Views: 31351

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

With plot you have to manually define the x values and compute the corresponding y given by the function.

>> x = 0:.01:1;
>> y = sin(10*x);
>> plot(x,y,'.-')

enter image description here

With fplot you define the function generically, for example as an anonymous function; pass a handle to that function; and let Matlab choose the x values and compute the y values. As an example, take a difficult function:

>> f = @(x) sin(1/x);

Assume we want to plot that between 0.01 and 1:

>> lims = [.01 1];
>> fplot(f, lims, '.-')

See how Matlab does a pretty good job choosing closer x values in the left area, where the function becomes wilder.

enter image description here

Upvotes: 7

Related Questions