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

2 次查看(过去 30 天)
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 中查找有关 Debugging and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by