Why do I get this error for my gauss seidel code?

조회 수: 2 (최근 30일)
Peter Phung
Peter Phung 2017년 11월 6일
답변: David Goodmanson 2017년 11월 6일
a = [3 -.1 -.2; .1 7 -.3; .3 -.2 10];
b = [7.85; -19.3; 71.4];
x = [0;0;0];
imax = 10;
es = 1e-6;
lambda = 0.5;
n = 3;
for i = 1:n
dummy = (a(i,i))
for j = 1:n
a(i,j) = a(i,j)/dummy
end
b(i) = b(i)/dummy
end
for i = 1:n
sum = b(i)
for j = 1:n
if i ~=j
sum = sum - (a(i,j)*x(j))
end
x(i) = sum
end
iter = 1;
while (1)
sentinel = 1;
for i = 1:n
old = x(i)
sum = b(i)
for j = 1:n
if i~= j
sum = sum - a(i,j)*x(j)
end
x(i) = lambda*sum+(1-lambda)*old
if sentinel = 1 && x(i) ~= 0
ea = abs((x(i)-old)/x(i))*100
if ea > es
sentinel = 0;
end
end
iter = iter + 1;
if sentinel = 1 | iter >= imax
break
end
end
end
The error I keep receiving is:
>> gaussseidel
Error: File: gaussseidel.m Line: 35 Column: 25
The expression to the left of the equals sign is not a valid target for an assignment.
Why is this?

답변 (1개)

David Goodmanson
David Goodmanson 2017년 11월 6일
Hello Peter, try
if sentinel == 1 && x(i) ~= 0
instead of
if sentinel = 1 && x(i) ~= 0

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by