How do I find a root in a nonlinear equation?
조회 수: 1 (최근 30일)
이전 댓글 표시
my equation is
function y=f(x)
x = (-20:0.05:20); %-20<x<20
y=5*(x^2)*(sin(x))
I already have the graph of this function, but what would I write in the script (m file) to solve this equation for the root and have it plot on the same graph? I tried fzero, but I keep getting an error message back, so maybe I'm using it incorrectly?
댓글 수: 0
답변 (1개)
Star Strider
2015년 4월 29일
One option:
x = (-20:0.05:20); %-20<x<20
y=5*(x.^2).*(sin(x));
yz = y.*circshift(y, [0 -1]);
yzi = find(yz < 0);
for k1 = 1:size(yzi,2)-1
xzeros(k1) = interp1(y(yzi(k1):yzi(k1)+1),x(yzi(k1):yzi(k1)+1),0);
end
figure(1)
plot(x, y, '-m')
hold on
plot(xzeros, zeros(size(xzeros)), 'pb')
hold off
grid
The routine finds the indices near the zero-crossings by multiplying the y-values with a one-index circularly-shifted version of itself and then finds the values where these products are negative. It then uses interp1 and the intervals defined by these indices to find the actual values of the zeros, producing the vector ‘xzeros’ that are the x-coordinates of the zeros. It then plots them.
댓글 수: 2
Star Strider
2015년 4월 29일
True. The consecutive indices though provide enough information for interp1 to find the actual zeros.
참고 항목
카테고리
Help Center 및 File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!