ellipse - Plot solution of second order equation in MATLAB -
could please me following question: want solve second order equation 2 unknowns , use results plot ellipse. here function:
fun = @(x) [x(1) x(2)]*v*[x(1) x(2)]'-c v 2x2 symmetric matrix, c positive constant , there 2 unknowns, x1 , x2. if solve equation using fsolve, notice solution sensitive initial values
fsolve(fun, [1 1]) is possible solution equation without providing exact starting value, rather range? example, see possible combinations x1, x2 \in (-4,4)
using ezplot obtain desired graphical output, not solution of equation.
fh= @(x1,x2) [x1 x2]*v*[x1 x2]'-c; ezplot(fh) axis equal is there way have both? lot!
you can take xdata , ydata ezplot:
c = rand; v = rand(2); v = v + v'; fh= @(x1,x2) [x1 x2]*v*[x1 x2]'-c; h = ezplot(fh,[-4,4,-4,4]); % plot in range axis equal fun = @(x) [x(1) x(2)]*v*[x(1) x(2)]'-c; x = fsolve(fun, [1 1]); % specific solution hold on; plot(x(1),x(2),'or'); % possible solutions in range x1 = h.xdata; x2 = h.ydata; or can use vector input fsolve:
c = rand; v = rand(2); v = v + v'; x1 = linspace(-4,4,100)'; fun2 = @(x2) sum(([x1 x2]*v).*[x1 x2],2)-c; x2 = fsolve(fun2, ones(size(x1))); % remove invalid values tol = 1e-2; x2(abs(fun2(x2)) > tol) = nan; plot(x1,x2,'.b') however, easiest , straight forward approach rearrange ellipse matrix form in quadratic equation form:
k = rand; v = rand(2); v = v + v'; = v(1,1); b = v(1,2); c = v(2,2); % rearange terms in form of quadratic equation: % a*x1^2 + (2*b*x2)*x1 + (c*x2^2) = k; % a*x1^2 + (2*b*x2)*x1 + (c*x2^2 - k) = 0; x2 = linspace(-4,4,1000); = a; b = (2*b*x2); c = (c*x2.^2 - k); % solve regular quadratic equation dicriminant = b.^2 - 4*a.*c; x1_1 = (-b - sqrt(dicriminant))./(2*a); x1_2 = (-b + sqrt(dicriminant))./(2*a); x1_1(dicriminant < 0) = nan; x1_2(dicriminant < 0) = nan; % plot plot(x1_1,x2,'.b') hold on plot(x1_2,x2,'.g') hold off
Comments
Post a Comment