how to plot a function handle on matlab 7 ?

my code is i'm asking the user to input a funtion and i transform it to a handle one (note that i'm using the matlab 7.0 R14) :
funstr = input('• Please enter your function here : ', 's');
f = eval( ['@(x) ' funstr ] );
how can i possibly do the plot of this between the two points a and b? i keep getting errors
??? Index exceeds matrix dimensions
or
??? Error using ==> plot Conversion to double from function_handle is not possible.
i tried it with plot and fplot and none of them is working !
thanks for helping in advance.

 採用された回答

Star Strider
Star Strider 2018 年 4 月 20 日

3 投票

You need to evaluate the function in the plot call.

Example

fun = @(x) x.^3 - 8*x.^2 + 10;
x = linspace(-5, 5);
figure
plot(x, fun(x))
grid

6 件のコメント

HeadingUpHigh
HeadingUpHigh 2018 年 4 月 20 日

first of all thanks for your quick answer, i tried it but it keeps giving me this error : ??? Error using ==> plot

 Conversion to double from function_handle is not possible.
 Error in ==> SimpsonComposee at 136
      plot(x, f)
Star Strider
Star Strider 2018 年 4 月 20 日

My pleasure.

I have no idea what ‘f’ is. If it is a function of one variable, try this:

plot(x, f(x))

It is now no longer a function handle, but a function evaluation.

HeadingUpHigh
HeadingUpHigh 2018 年 4 月 20 日

thanks man it worked, also i have another question if possible :

      ax = gca;
      ax.XAxisLocation = 'origin';
      ax.YAxisLocation = 'origin';

this is to Set the y-axis so it passes through the origin of the x-axis (x = 0). but it's not working for me in matlab 7 what's wrong ?

Star Strider
Star Strider 2018 年 4 月 20 日

As always, my pleasure.

I believe it was introduced in R2016b.

There are other options that will work in previous versions, the most popular being axescenter (link). A full File Exchange search for similar functions is here (link).

HeadingUpHigh
HeadingUpHigh 2018 年 4 月 21 日
thanks mate, you've been very helpful.
Star Strider
Star Strider 2018 年 4 月 21 日
As always, my pleasure.
The File Exchange has several functions that add necessary capabilities missing in earlier MATLAB versions. (Many of these have been incorporated in MATLAB and the Toolboxes since.)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by