What am I doing wrong with syms?
이전 댓글 표시
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
Walter Roberson
2018년 10월 19일
You have a typo: sin(angle) should be sind(angle)
채택된 답변
추가 답변 (1개)
Serafeim Klimantiris
2018년 10월 21일
댓글 수: 1
Walter Roberson
2018년 10월 22일
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.
카테고리
도움말 센터 및 File Exchange에서 Number Theory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!