Confusion on function handles

Greetings,
I know this is a tricky subject for me to understand, but I want to see if I'm going about this right.
I have the following three functions:
f(x)= x^8 + 8*x^7 + 28*x^6 + 56*x^5 +70*x^4 - 56*x^3 + 28*x^2 - 8*x +1
g(x)=(((((((x-8)*x + 28) - 56)*x + 70) - 56)*x + 28)*x - 8)*x + 1
h(x)=(x-1)^8
that need to be evaluated at 101 equally spaced points in the interval [0.99,1.01] and plot the results on the magnified scale.
Now, can I use function handles with this, i.e. (as a start),
f =@(x) x^8 + 8*x^7 + 28*x^6 + 56*x^5 +70*x^4 - 56*x^3 + 28*x^2 - 8*x +1
g =@(x) (((((((x-8)*x + 28) - 56)*x + 70) - 56)*x + 28)*x - 8)*x + 1
h =@(x) (x-1)^8
n = 100;
a = 0.99;
b = 1.01;
t = a:(b-a)/n:b;
Or am I way off?
Thanks!

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 26 日
Jesse, if you want to add a comment click on [comment on this answer]

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

回答 (2 件)

Shashank Prasanna
Shashank Prasanna 2013 年 9 月 26 日
編集済み: Shashank Prasanna 2013 年 9 月 26 日

2 投票

You need to precede your operations with a dot "." as described here:
Then create your anonymous function handles.
Create linearly spaced points using LINSPACE:
x = linspace(0.99,1.01,100);
Call the function with x as input

2 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 26 日
Jesse commented
Shashank,
Thanks for the response. Forgot about the dots! So, like this?
f =@(x) x.^8 + 8*x.^7 + 28*x.^6 + 56*x.^5 +70*x.^4 - 56*x.^3 + 28*x.^2 - 8*x +1 g =@(x) (((((((x-8)*x + 28) - 56)*x + 70) - 56)*x + 28)*x - 8)*x + 1 h =@(x) (x-1).^8
x = linspace(0.99,1.01,100);
Shouldn't the 100 be 101 for 101 equally spaced points?
So how is the call made in this fashion?
Thanks again!
Shashank Prasanna
Shashank Prasanna 2013 年 9 月 27 日
Jesse, here is an example, you can follow the same approach for any anonymous function handles that has been defined the proper way to do elementwise operations:
h =@(x) (x-1).^8;
x = linspace(0.99,1.01,100);
plot(x,h(x))

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

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 26 日

0 投票

for k=1:numel(t)
y(k)=f(t(k))+g(t(k))+h(t(k))
end
plot(t,y)

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 26 日
Jesse commented
Azzi,
I take it I still need the dots to definte t(k)? How is t(k) defined? Thanks!

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

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2013 年 9 月 26 日

コメント済み:

2013 年 9 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by