FPLOT, how to define step size along X-axis?

50 ビュー (過去 30 日間)
Marco
Marco 2013 年 10 月 17 日
コメント済み: Vsevolod Kolobov 2022 年 4 月 2 日
I try to understand the possibilities of FPLOT and consulted the documentation (relevant phrases cited below). But I don´t understand how the step size along the X-axis, for which the Y values are then calculated, becomes automatically defined or can be manually defined.
On page 17-21 in the MATLAB R2013b Programming Fundamentals documentation it is given an example which makes use of FPLOT:
s = @(x) sin(1./x);
range = [0.01,0.1];
fplot(s,range)
In the documentation for the FPLOT function (help fplot) I then find this:
fplot(FUN,LIMS,N) with N >= 1 plots the function with a minimum of N+1 points. The default N is 1. The maximum step size is restricted to be (1/N)*(XMAX-XMIN).
[X,Y] = fplot(FUN,LIMS,...) returns X and Y such that Y = FUN(X)
NOW MY OBSERVATION: If coding the following:
s = @(x) sin(1./x);
range = [0.01,0.1];
[axisX,axisY]= plot(s,range)
then I receive for the X-axis 377 values registered. While MATLAB decided that the function should be plotted based on 377 calculated steps, I for instance would like it to become calculated in the given range in 20 steps along the X-axis and therefore tried to type:
clear axisX axisY;
[axisX,axisY]=fplot(s,range,20)
But I again receive 377 values registered, but not as expected only 20. Could you explain me, how the step size along the X-axis, for which the Y values are then calculated, becomes automatically defined or can be manually defined within/by the FPLOT syntax?
  2 件のコメント
Vivek Selvam
Vivek Selvam 2013 年 10 月 17 日
Under the fplot tips in the documentation you can see:
fplot uses adaptive step control to produce a representative graph, concentrating its evaluation in regions where the function's rate of change is the greatest.
  1. Therefore the step size is automatically calculated and can't be set.
  2. If you give N, then fplot plot the function with at least N + 1 points (which gives you the maximum step size of (xmax-xmin)/N).
  3. If you want to set a step size, feval is the way to go as Jonathan suggested.
Marco
Marco 2013 年 10 月 18 日
Now I understand why Jonathan suggested the different approach using linspace and feval functions for solving my problem instead of mentioning any 'correct' fplot syntax for it. Such syntax seem to just not exist, and the different approach is the way to anyway help me. Thanks, Vivek!

サインインしてコメントする。

採用された回答

Jonathan LeSage
Jonathan LeSage 2013 年 10 月 17 日
編集済み: Jonathan LeSage 2013 年 10 月 17 日
To define your own points, you could just use the feval function. You could define your own input vector (with say, 20 steps) and compute an output with an equal number of elements. From there, you can just plot the results!
Taking your example:
% Function to plot
s = @(x) sin(1./x);
% Defining values over which to evaluate the function
numPoints = 20;
xvec = linspace(0.01,0.1,numPoints);
% Evaluate via 'feval' and plot
yvec = feval(s,xvec);
plot(xvec,yvec)
Hopefully this helps to get you started!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by