Sorry everyone. I'm pretty new to matlab. My question was to graph an equation V against x for -4</= x </=4
This is the code I came up with:
x=linspace(-4,4);
V=(x.^3/3)-4*x;
plot(x,V)
xlabel('Position'),ylabel('Potential Energy')
I need to mark the x-intercepts on the graph. How do I go about doing that?

 채택된 답변

Adam Danz
Adam Danz 2019년 11월 15일
편집: Adam Danz 2019년 11월 16일

6 개 추천

Method 1: solve equation for y=0
Use the Symbolic Math Toolbox to solve for y=0; see inline comments for details.
% Solve for y=0
syms x
eqn = x.^3/3-4*x == 0;
xInt = double(solve(eqn)); % X values where y=0
yInt = zeros(size(xInt)); % Corresponding y values (all 0)
% plot function and x-intercepts
x=linspace(-4,4);
V=(x.^3/3)-4*x;
plot(x,V,'k-')
hold on
plot(xInt,yInt, 'm*','MarkerSize', 10)
yline(0)
Method 2: use intersections() to find x-intercepts
This uses the intersections() function from the file exchange to find the (x,y) coordinates of the x-intercepts.
x=linspace(-4,4);
V=(x.^3/3)-4*x;
[xInt,yInt] = intersections(x,V,x,zeros(size(V)));
% ^^ ^^ There are your intercept coordinates
plot(x,V,'k-')
hold on
plot(xInt,yInt, 'm*','MarkerSize', 10)
yline(0)
Both methods produce this figure

댓글 수: 8

Daniel Matthew
Daniel Matthew 2019년 11월 15일
Thank you!
Adam Danz
Adam Danz 2019년 11월 16일
@Daniel Matthew, I just added "Method 1" to my answer. It uses the Symbolic Math Toolbox to solve your equation for y=0 and therefore does not depend on a 3rd party FEX function.
Abdennaser Hadab
Abdennaser Hadab 2022년 10월 15일
The second method pops an error:
Undefined function 'intersections' for input arguments of type 'double'.
Any idea what is wrong?
Adam Danz
Adam Danz 2022년 10월 17일
@Abdennaser Hadab, the sentence under the "Method 2" heading tells you the answer and has a link :)
Abdennaser Hadab
Abdennaser Hadab 2022년 10월 18일
편집: Abdennaser Hadab 2022년 10월 18일
@Adam Danz thanks, I have been a bit confused but I realized I needed to download the file and put it in the same folder where my .m file is in order to call for the function with no issues.
I did not know I had to do this.
Adam Danz
Adam Danz 2022년 10월 18일
@Abdennaser Hadab, you figured it out! You can either add the file to the same folder containing the main m-file or you can store the file anywhere and make sure you add the path to the file using addpath in the m-file.
Abdennaser Hadab
Abdennaser Hadab 2022년 10월 19일
@Adam Danz thank for the tip!
Shyamini
Shyamini 2024년 1월 24일
@Adam Danz Thank you so much!! this helped tremendously

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2019년 11월 15일

댓글:

2024년 1월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by