MATLAB Answers

0

Marking x intercepts on graph

Daniel Matthew さんによって質問されました 2019 年 11 月 15 日 19:24
最新アクティビティ Adam Danz
さんによって コメントされました 2019 年 11 月 16 日 20:06
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?

  0 件のコメント

サインイン to comment.

タグ

製品


リリース

R2019b

1 件の回答

Adam Danz
回答者: Adam Danz
2019 年 11 月 15 日 20:09
編集済み: Adam Danz
2019 年 11 月 16 日 20:06
 採用された回答

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

  2 件のコメント

Daniel Matthew 2019 年 11 月 15 日 20:14
Thank you!
Adam Danz
2019 年 11 月 16 日 20:06
@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.

サインイン to comment.



Translated by