在MATLAB中绘制二阶方程的解 [英] Plot solution of second order equation in MATLAB

查看:174
本文介绍了在MATLAB中绘制二阶方程的解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您帮我解决以下问题: 我想用两个未知数求解一个二阶方程,并使用结果绘制一个椭圆. 这是我的功能:

Could you please help me with the following question: I want to solve a second order equation with two unknowns and use the results to plot an ellipse. Here is my function:

fun = @(x) [x(1) x(2)]*V*[x(1) x(2)]'-c

V is 2x2对称矩阵,c是一个正常数,并且有两个未知数,x1x2. 如果我使用fsolve求解方程,我会注意到该解决方案对初始值非常敏感

V is 2x2 symmetric matrix, c is a positive constant and there are two unknowns, x1 and x2. If I solve the equation using fsolve, I notice that the solution is very sensitive to the initial values

fsolve(fun, [1 1])

是否有可能在不提供确切起始值而在一定范围内的情况下获得此方程式的解?例如,我想查看x1, x2 \in (-4,4)

Is it possible to get the solution to this equation without providing an exact starting value, but rather a range? For example, I would like to see the possible combinations for x1, x2 \in (-4,4)

使用ezplot我获得所需的图形输出,但未获得方程式的解.

Using ezplot I obtain the desired graphical output, but not the solution of the equation.

fh= @(x1,x2) [x1 x2]*V*[x1 x2]'-c;
ezplot(fh)
axis equal

有两种方法都可以吗? 非常感谢!

Is there a way to have both? Thanks a lot!

推荐答案

,您可以从ezplot中获取XDataYData:

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;

或者您可以将向量输入用于fsolve:

or you can use vector input to 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, the easiest and most straight forward approach is to rearrange the ellipse matrix form in a quadratic equation form:

k = rand;
V = rand(2);
V = V + V';
a = V(1,1);
b = V(1,2);
c = V(2,2);
% rearange terms in the 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 = 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

这篇关于在MATLAB中绘制二阶方程的解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆