0
votes

I want to solve equations in matlab, eg.

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

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

and the code would be:

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

However, if I want to take 100 as a variable, like

for k=1:100

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

end

There would be an error, how to make it?

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

I got the expression value, how do I get the numeric value of x,y,z?

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

not work ,shows ??? Error using ==> mupadmex Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.

If the input expression contains a symbolic variable, use the VPA function instead.

Error in ==> sym.sym>sym.double at 927 Xstr = mupadmex('mllib::double', S.s, 0);

Error in ==> f2 at 38 [['x(1)=';'x(2)='],num2str(double(sol.x))]

2
Do you have the symbolic toolkit? - John Alexiou
Take the solve out of the loop and uses the subs command to substitute values in the loop. - John Alexiou

2 Answers

1
votes

If you have access to the Symbolic Toolkit then you do the following:

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

Edit 1

Based on new code by OP the matlab script to solve this is:

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);
0
votes

I would try

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

and then solve the equation