Question

f(x)=x^2+sin(x)+1/x Find f(0), f(1) and f(π/2) Vectorize f and evaluate f(x) where x=[0 1 π/2 π]....

f(x)=x^2+sin(x)+1/x

  1. Find f(0), f(1) and f(π/2)
  2. Vectorize f and evaluate f(x) where x=[0 1 π/2 π].
  3. Create x=linspace(-1,1), evaluate f(x),
  4. plot x vs f(x) for x is 20 equally spaced values between 11 and 20.
  5. Use fplot to graph f(x) over x from – π to π.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • HERE IS THE MATLAB CODE FOR ALL 3 PARTS:

clear; close; clc;

f=@(x) x^2+sin(x)+1/x;

%% part 1

disp('f(0)');

disp(f(0));

disp('f(1)');

disp(f(1));

disp('f(2)');

disp(f(2));

%% part 2

x1=[0,1,pi/2,pi];

disp('Vectorised f(x)');

disp(arrayfun(f,x1));

%% part 3

x2=linspace(-1,1);

disp('f(x) with linspace');

disp(arrayfun(f,x2));

%% part 4

l=11;

u=20;

delta=(u-l)/20;

x3=l:delta:u;

disp('f(x) with 20 equally spaced values');

disp(arrayfun(f,x3));

%% part 5

fplot(f,[-pi pi])

  • MATLAB RESULTS:-

f(0)
Inf

f(1)
2.8415

f(2)
5.4093

Vectorised f(x)
Inf 2.8415 4.1040 10.1879

f(x) with linspace
Columns 1 through 12

-0.8415 -0.8910 -0.9402 -0.9893 -1.0381 -1.0869 -1.1356 -1.1845 -1.2334 -1.2827 -1.3323 -1.3825

Columns 13 through 24

-1.4332 -1.4848 -1.5373 -1.5909 -1.6459 -1.7024 -1.7607 -1.8212 -1.8841 -1.9498 -2.0188 -2.0915

Columns 25 through 36

-2.1685 -2.2504 -2.3381 -2.4324 -2.5345 -2.6455 -2.7671 -2.9011 -3.0498 -3.2161 -3.4035 -3.6167

Columns 37 through 48

-3.8616 -4.1461 -4.4806 -4.8798 -5.3644 -5.9649 -6.7280 -7.7291 -9.0985 -11.0825 -14.2085 -19.8479

Columns 49 through 60

-33.0294 -99.0100 99.0102 33.0312 19.8530 14.2185 11.0990 9.1232 7.7636 6.7739 6.0239 5.4381

Columns 61 through 72

4.9698 4.5886 4.2736 4.0104 3.7884 3.5996 3.4383 3.2998 3.1805 3.0775 2.9886 2.9118

Columns 73 through 84

2.8457 2.7889 2.7404 2.6992 2.6647 2.6361 2.6128 2.5944 2.5805 2.5707 2.5646 2.5619

Columns 85 through 96

2.5624 2.5660 2.5722 2.5811 2.5924 2.6059 2.6215 2.6392 2.6588 2.6802 2.7032 2.7279

Columns 97 through 100

2.7542 2.7819 2.8110 2.8415

f(x) with 20 equally spaced values
Columns 1 through 11

120.0909 130.2913 141.0759 152.3888 164.1496 176.2696 188.6689 201.2931 214.1233 227.1805 240.5210

Columns 12 through 21

254.2255 268.3829 283.0724 298.3480 314.2278 330.6901 347.6779 365.1102 382.8982 400.9629

  • THE FPLOT

15 10 -5 10 15 -3 -2

Add a comment
Know the answer?
Add Answer to:
f(x)=x^2+sin(x)+1/x Find f(0), f(1) and f(π/2) Vectorize f and evaluate f(x) where x=[0 1 π/2 π]....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT