how do i keep symbolic solver from creating a 1x3 sym instead of 1x1 sym that I want

2 views (last 30 days)
Can someone tell me why this code creates the equation as a 1x3 sym instead of a 1x1. I figured out that is has to do with the iterations of OA however other people have the same code as I do and it creates a 1x1 sym for them. I need to be able to find the value of OB for use in later parts of the code and since the current code creates a 1x3 sym, all of the corresponding values are empty structs rather than real solved for values.
Here is my code with the initial values lined up:
AB = .40;
phi = 120;
theta = [0:.5:360];
OA = [.10 .20 .30];
%start OA loop
for i = 1:3
i = 1;
for theta = 0:360
%find OA_
OA_ = OA(i) *[cosd(theta) sind(theta) 0];
%find AC_
syms OB
OB_eq = OA(i)^2+OB^2-AB^2-2*OA*OB*cosd(theta);
sol = solve(OB_eq);
OB = eval(sol);
Hopefully someone can sus out what is going wrong here. I even tested a seperate symbolic solver equation useing one iterative value like in this equation and it created a 1x1 sym that was solvable
edit:grammar

Answers (1)

Walter Roberson
Walter Roberson on 28 Sep 2021
Well, that is not right. You should be getting two solutions each. You have a quadratic equation, so you should expect two solutions each. In your case, one of the solutions is negative and the othe is positive, but all that you asked for is that the values be "real", so both of them are valid solutions.
AB = .40;
phi = 120;
theta_vals = 0:5:360; %0:0.5:360
OA = [.10 .20 .30].';
num_OA = length(OA);
num_theta = length(theta_vals);
OB_numeric = zeros(num_theta, num_OA, 2);
%start OA loop
for theta_idx = 1 : num_theta
theta = theta_vals(theta_idx);
%find OA_
OA_ = OA .* [cosd(theta) sind(theta) 0];
%find AC_
syms OB
OB_eq = OA.^2 + OB.^2 - AB.^2 - 2 .* OA .* OB .* cosd(theta);
sol = arrayfun(@solve, OB_eq.', 'uniform', 0);
sol = horzcat(sol{:});
OB_numeric(theta_idx, :, :) = sol.';
end
mask = all(OB_numeric > 0, 3);
nnz(mask)
ans = 0
The output array, OB_numeric, will have one row for each theta value, and will have one column for each OA value, and will have two hyperplanes for the two solutions to the quadratic.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!