MATLAB Answers

Is it possible to rewrite these equation set to matrix form?

1 view (last 30 days)
wei zhang
wei zhang on 26 Jun 2020
Answered: Ameer Hamza on 26 Jun 2020
I am dealing with some equation set to calculate coefficients b. The equation set is as below. The x y z are the inputs. b is output.
syms x [4 1]
syms y [4 1]
syms z [4 1]
b = zeros(4,1);
b(1) = y3*z2 - y2*z3 + y2*z4 - y4*z2 - y3*z4 + y4*z3;
b(2) = y1*z3 - y3*z1 - y1*z4 + y4*z1 + y3*z4 - y4*z3;
b(3) = y2*z1 - y1*z2 + y1*z4 - y4*z1 - y2*z4 + y4*z2;
b(4) = y1*z2 - y2*z1 - y1*z3 + y3*z1 + y2*z3 - y3*z2;
The equation set would be calculation many times. It takes long time. I want to speed it up. I would like to rewrite this equation set into matrix multiplication, like b = A*[x,y,z] . So I could calculate the vector b in one time , not 4 times. Is it possible? I think this may need some math mind, I have not figure it out. Please help me.
I also try to use subs function. But it is very slow.

  0 Comments

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 26 Jun 2020
These variables are nonlinearly related, so not sure how do you want to write in matrix form. Also, it is unlikely that it will cause a significant improvement in speed. You are using a symbolic toolbox that is inherently slow as compared to finite-precision floating-point maths. If you know the values of x, y, and z and want to find values of b, then I suggest you use regular MATLAB operators. For example
syms x [4 1]
syms y [4 1]
syms z [4 1]
b = sym(zeros(4,1));
b(1) = y3*z2 - y2*z3 + y2*z4 - y4*z2 - y3*z4 + y4*z3;
b(2) = y1*z3 - y3*z1 - y1*z4 + y4*z1 + y3*z4 - y4*z3;
b(3) = y2*z1 - y1*z2 + y1*z4 - y4*z1 - y2*z4 + y4*z2;
b(4) = y1*z2 - y2*z1 - y1*z3 + y3*z1 + y2*z3 - y3*z2;
b_fun = matlabFunction(b, 'Vars', {[x1 x2 x3 x4], [y1 y2 y3 y4], [z1 z2 z3 z4]});
x_vec = rand(1,4);
y_vec = rand(1,4);
z_vec = rand(1,4);
b_val = b_fun(x_vec, y_vec, z_vec)

  0 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!