MathGuy101
MathGuy101

Reputation: 23

Inaccurate plot in MATLAB, not showing correct points

I am trying to plot a function in MATLAB and mark the root with an 'x'. The function I am plotting is

  y = (1+(sqrt(9-x.^2)/sqrt(4-x.^2))-sqrt(9-x.^2))  

here is my code:

x = 0:0.00001:2;
y =  1+(sqrt(9-x.^2)/sqrt(4-x.^2))-sqrt(9-x.^2);
x_marker = 1.2311;
y_marker = 0;
plot(x,y,'-',x_marker,y_marker,'x'),grid;

The root of my function is approximately x = 1.2311 however the MATLAB plot shows the root to be around 1.4. I have no idea why this happening. Any help is appreciated, thanks.

Upvotes: 1

Views: 167

Answers (1)

Dan
Dan

Reputation: 45752

The problem is because you are using matrix division, /, instead of element-wise division: ./.

Define y as

y =  1+(sqrt(9-x.^2)./sqrt(4-x.^2))-sqrt(9-x.^2);  %// note the . that has been added

And you will get the graph you expect. (note you should probably adjust the range of x to end at around 1.5)

Upvotes: 2

Related Questions