MATLAB: What am I doing wrong with syms

Symbolic Math Toolboxsyms

Hello everybody I am trying to make a program that simulates a ray going through a 2d matrix on a set angle, and then highlight the path. When I get to the syms part, I get the "Unable to find explicit solution" error many times. What am I doing wrong there? Thanks in advance!!
clc
clear all
close all
a=randi([0,64],[6,6]); %create a 6*6 matrix with random values
first_row=1; %set entry point coords

entrypoint=3; %set entry point coords
a(first_row,entrypoint)=0; %pinpoint the entry point in the matrix
angle=45; %set an angle
y(1)=entrypoint; %we're gonna need these in the loop

x(1)=first_row; %we're gonna need these in the loop
for i=2:1:length(a) %repeat till it reaches the end of the matrix
syms x(i) y(i) %define the 2 unknown factors
x(i)=(((y(i)-y(i-1))*(sind(90-angle))/sin(angle))+x(i-1)); %based on A/sin(a)=B/sin(b)
y(i)=((x(i)-x(i-1))*tand(angle)+y(i-1));%based on A/sin(a)=B/sin(b)
t=floor(solve(x(i))); %solve for y
z=floor(solve(y(i))); %solve for x
a(t,z)=0; %highlight the "path" on matrix a
end
t %check


z %check
a %check
image(a)

Best Answer

You have

 for i=2:1:length(a)  %repeat till it reaches the end of the matrix 

so you expect i to be an integer value.

But on the next line you have

syms x(i) y(i)    %define the 2 unknown factors

which is the same as

i = sym('i');
x = symfun(sym('x(i)'), i);
y = symfun(sym('y(i)'), i);

so i becomes an unresolved symbolic value and x and y become symbolic functions.

And that means that when you get to

    x(i)=(((y(i)-y(i-1))*(sind(90-angle))/sind(angle))+x(i-1));  %based on A/sin(a)=B/sin(b) 

then x becomes a symbolic function with parameter i, involving y evaluated at the unresolved symbol i and i-1, and also involving the previous symbolic function x (that has no definition) evaluated at i-1 .

Then

    y(i)=((x(i)-x(i-1))*tand(angle)+y(i-1));%based on A/sin(a)=B/sin(b)

y becomes a symbolic function with parameter i, involving the just-defined symbolic function x(i) evaluated at a couple of inputs, and involving the previous symbolic function y (that has no definition) evaluated at i-1 .

This is a mess, and it is not clear what your intentions are.

Perhaps the following will be meaningful to you:

a = randi([0,64],[6,6]); %create a 6*6 matrix with random values
first_row=1; %set entry point coords
entrypoint=3; %set entry point coords
a(first_row,entrypoint)=0;    %pinpoint the entry point in the matrix
angle=45;   %set an angle
syms x y yi
y(1)=entrypoint;  %we're gonna need these in the loop
x(1)=first_row;    %we're gonna need these in the loop
for i=2:1:length(a)  %repeat till it reaches the end of the matrix 
  x(i)=(((yi-y(i-1))*(sind(90-angle))/sind(angle))+x(i-1));  %based on A/sin(a)=B/sin(b) 
  y(i)=((x(i)-x(i-1))*tand(angle)+y(i-1));%based on A/sin(a)=B/sin(b)
  t = double(floor(solve( subs(x(i), yi, y(i)))));  %solve for y
  z = double(floor(solve(y(i))));  %solve for x
  if t > 0 && z > 0
    a(t,z)=0;     %highlight the "path" on matrix a
  else
      fprintf('At iteration #%d, solution outside matrix: (t,z) = (%d,%d)\n', i, t, z);
  end
end

Chances are that it won't be what you want...

Related Question