What am I doing wrong with syms?
Show older comments
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)
1 Comment
Walter Roberson
on 19 Oct 2018
You have a typo: sin(angle) should be sind(angle)
Accepted Answer
More Answers (1)
Serafeim Klimantiris
on 21 Oct 2018
1 Comment
Walter Roberson
on 22 Oct 2018
You should rarely be passing an expression for the second argument of subs() . Your expression
x(i)=subs(x(i), y(i), ((xi-x(i-1))*tand(angle))+y(i-1) );
is saying to look into x(i) for expressions that just happen to have the same substructure as the expression y(i) does, and change those places to be ((xi-x(i-1))*tand(angle))+y(i-1) instead. If the substructure does not appear exactly the same (perhaps because numeric coefficients were combined) then nothing would be substituted.
And in
ysol=round(solve(x(i),y(i)))
you are asking to solve the expression x(i) for the expression stored in y(i) -- what does that mean?? Especially after the subs() you did which would have replaced y(i) with something else.
Categories
Find more on Number Theory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!