Matlab Plotting Error
2 views (last 30 days)
Show older comments
I'm trying to find the roots for an equation but cannot get the correct answer. I tried plotting the function in Matlab over an interval that would include all roots, but did not get the plot that I would expect for such a function. Also, when using fzero for the function and appropriate guesses, I get incorrect answers for the roots. Upon copying and pasting the same function into another (unnamed) program, I received the correct roots using the same initial guesses. Is there an error in my function syntax or is it in Matlab??
mu=0.01215;
x=[-1:3];
f1= -1*(1-mu).*(x+mu)/((sqrt((x+mu).^2)).^3) - mu.*(x-1+mu)/((sqrt((x-1+mu).^2)).^3) + x;
plot(x,f1)
It plots a linear equation, but it should have oblique and vertical asymptotes.
I wasted about two hours of time trying to find errors on something that should have taken about 10 minutes, only to find out it worked elsewhere.
0 Comments
Answers (1)
Walter Roberson
on 18 Nov 2011
You should refine your sampling points.
[-1:3] does not denote a closed interval in MATLAB: it is the same thing as [-1 0 1 2 3] which is only 5 sampling points.
Try
x = linspace(-1,3,50);
2 Comments
Walter Roberson
on 18 Nov 2011
Incorrect vectorization. You missed the "." before the "/" in both places.
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!