First, I would suggest that you use an Anonymous Function for ‘f’. They make life immeasurably easier in situations such as yours. Second, you need to do element-by-element division, as well as exponentiation. Use (./) instead of (/) here.
A slightly shorter version of your code seems to work for me:
f = @(x) (0.5*x.^3-x.^2)./(x.^2-x-20);
x = -15:0.1:15;
y = f(x);
figure(1)
plot(x, y)
grid
You can create your ‘x1’ and ‘y1’ and other vectors just as you did in your original code, just easier. Use the one anonymous function version for ‘f’ to calculate all your ‘y1’ from ‘x1’ and so for the rest.
Best Answer