Use vpasolve for vectors of two inputs.

14 views (last 30 days)
Hello,
I have the inputs A and B as vectors. Moreover I got the constants c and d. My equotations look like (c-d-x)^2+y^2=d^2 and x^2+(c-d-y)^2=d^2. Those should give for each row of the input vector two pairs of solutions (x(1),y(1) and x(2),y(2)).
I found a solution with a for loop, but vpasolve has to run a lot of times, which consumes a lot of time:
A=[1,2,3,4];
B=[5,6,7,8];
c=9;
d=10;
%those numbers are just for the example and do not lead to real solution
for i=1:length(A)
clear x
clear y
syms x y
S = vpasolve([(c-A(i)-x)^2+y^2==d^2, x^2+(c-B(i)-y)^2==d^2],[x,y]);
L(1,:,i)=[double(S.x(1));double(S.y(1))];
L(2,:,i)=[double(S.x(2));double(S.y(2))];
end
This however takes some time for a long input vector, so I do wonder if it is possible to speed this up (maybe get rid of the for-loop)?
Thank you in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Jun 2015
A = [1, 2, 3, 4];
B = [5, 6, 7, 8];
c=9;
d=10;
syms As Bs x y
sol = solve((c-As-x)^2+y^2==d^2, x^2+(c-Bs-y)^2==d^2,x,y);
Rx = double(subs(sol.x, {As, Bs}, {A, B}));
Ry = double(subs(sol.y, {As, Bs}, {A, B}));
I am not sure what the output dimensions of Rx and Ry would be.
  1 Comment
AndreasO
AndreasO on 30 Jun 2015
Thanks for the answer.
The code works very well for me. Rx and Ry output vectors with twice the length of A/B, in which the first entries are always c/d and can be deleted.
So i had to add:
Rx(1:length(Rx)/2)=[];
Ry(1:length(Ry)/2)=[];
And i was able to get very quickly the solutions for my parameters.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!