0

我想在matlab中求解方程,例如。

100+a/2=173*cos(b) 

sqrt(3)*a/2=173*sin(b)

代码是:

[a,b]=solve('100+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')

但是,如果我想将 100 作为变量,例如

for k=1:100

  [a,b]=solve('k+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')

end

会有错误,怎么弄?

degree=140/1000000;
p=42164000;
a=6378136.5;
b=6356751.8;
x_1=0;
y_1=p;
z_1=0;

for i=451:550
    for j=451:550
    alpha=(1145-i)*degree;
    beta=(1145-j)*degree;

    x_2=p/cos(alpha)*tan(beta);
    y_2=0;
    z_2=p*tan(alpha);

    syms x y z x_1 x_2 y_1 y_2 z_1 z_2 a b
    eq = [(x-x_1)*(y2-y_1)-(x_2-x_1)*(y-y_1),(x-x_1)*(z_2-z_1)-(x_2-x_1)*(z-z_1), b^2*(x^2+y^2)+a^2*(y^2)-a^2*b^2 ]; 
    sol = solve(eq(1),x,eq(2),y, eq(3),z); 
    sol.x
    sol.y
    sol.z        
   end
end

我得到了表达式值,我如何得到 x,y,z 的数值?

[['x(1)=';'x(2)='],num2str(double(sol.x))]

不工作,显示???使用 ==> mupadmex 时出错 MuPAD 命令中的错误:DOUBLE 无法将输入表达式转换为双精度数组。

如果输入表达式包含符号变量,请改用 VPA 函数。

==> sym.sym>sym.double 中的错误 927 Xstr = mupadmex('mllib::double', Ss, 0);

==> f2 在 38 处出错 [['x(1)=';'x(2)='],num2str(double(sol.x))]

4

2 回答 2

1

如果您有权访问 Symbolic Toolkit,那么您可以执行以下操作:

syms a b k
eq = [k+a/2-173*cos(b), sqrt(3)*a/2-173*sin(b)];
sol = solve(eq(1),a,eq(2),b);

sol.a = simplify(sol.a);
sol.b = simplify(sol.b);

% There are two solutions for 'a' and 'b'
% check residuals for example k=20

subs(subs(eq,{a,b},{sol.a(1),sol.b(1)}),k,20)
% ans = 0.2e-13

subs(subs(eq,{a,b},{sol.a(2),sol.b(2)}),k,20)
% ans = 0.2e-13

编辑 1

基于 OP 的新代码,解决此问题的 matlab 脚本是:

clear all
clc
syms alpha beta
degree=140/1000000;
p=42164000;
a=6378136.5;
b=6356751.8;
x_1=0;
y_1=p;
z_1=0;
x_2 = p/cos(alpha)*tan(beta);
y_2 = 0;
z_2 = p*tan(alpha);
syms x y z

eq = [(x-x_1)*(y_2-y_1)-(x_2-x_1)*(y-y_1);...
    (x-x_1)*(z_2-z_1)-(x_2-x_1)*(z-z_1); ...
    b^2*(x^2+y^2)+a^2*(y^2)-a^2*b^2 ];

sol = solve(eq(1),x,eq(2),y,eq(3),z);
sol.x  = simplify(sol.x);
sol.y  = simplify(sol.y);
sol.z  = simplify(sol.z);
pt_1 = [sol.x(1);sol.y(1);sol.z(1)]    % First Solution Point
pt_2 = [sol.x(2);sol.y(2);sol.z(2)]    % Second Solution Point
x = zeros(100,100);
y = zeros(100,100);
z = zeros(100,100);
for i=451:550
    disp(['i=',num2str(i)])
    for j=451:550        
        res = double(subs(pt_1,{alpha,beta},{(1145-i)*degree,(1145-j)*degree}));
        x(i-450, j-450) = res(1);
        y(i-450, j-450) = res(2);
        z(i-450, j-450) = res(3);
    end
end
disp('x=');
disp(x);
disp('y=');
disp(x);
disp('z=');
disp(x);
于 2012-09-21T13:25:04.017 回答
0

我会尝试

for i=1:100
    k=num2str(i)
    [a,b]=solve('100+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
end

然后解方程

于 2012-09-21T13:12:54.417 回答