matlab Invalid expression error

3 views (last 30 days)
Jakub Mis
Jakub Mis on 6 Apr 2021
Answered: Geoff Hayes on 6 Apr 2021
I need some help in solving the problem in this code that is:
Error: File: Automaty_komorkowe.m Line: 18 Column: 48
Invalid expression. Check for missing multiplication operator, missing or unbalanced
delimiters, or other syntax error. To construct matrices, use brackets instead of
parentheses.
This is right before:
axis square;
What should i do in this situation?
clear all
close all
n=100;
a=150;
T=400;
p=0.02;
g=0.5;
D1=ceil(a*rand(n,n));
D2=rand(n,n) < p;
D3=(rand(n,n) <= g)-1;
D4=D3 >= 0;
D= (D1.*D2).* D4+D3;
t=1
green=(D==0) | D<0;
blue= D>a | D<0;
all_colors=cat(3, red, green, blue);
image(all_colors); title(['t = ', num2str(t)]) axis square;
pause(5);
S(t) = sum(sum(D==0));
I(t) = sum(sum((D>0)&(D<=a)));
R(t) = sum(sum(D>a));
up=[2:n 1]; down=[n 1:n-1];
for t = 2:T
D1 = D(:,down);
D2 = D(:,up);
D3 = D(down,:);
D4 = D(up,:);
sick_neighbors = ((D1>0)&(D1<=a))+((D2>0)&(D2<=a)) + ((D3>0)&(D3<=a)) + ((D4>0)&(D4<=a));
stay_healthy = (D==0)&(sick_neighbors==0);
get_sick = ((D==0)&(sick_neighbors>0));
others = (D>0);
D(get_sick) = 1;
D(others) = D(others)+1;
S(t) = sum(sum(D==0));
I(t) = sum(sum((D>0)&(D<=a)));
R(t) = sum(sum(D>a));
green =(D==0) | D<0;
red =(D>0 & D<=a) | D<0;
blue = D>a | D<0;
all_colors = cat(3, red, green, blue);
image(all_colors); title(['t = ', num2str(t)])
axis square;
drawnow
end
figure
t=1:T;
plot(t,S,'green',t,I,'red',t,R,'blue')
legend('podatni','zainfekowani','odporni')
xlabel('czas')

Answers (1)

Geoff Hayes
Geoff Hayes on 6 Apr 2021
Jakub - try adding a semi-colon after
title(['t = ', num2str(t)]);
so that it is distinct from the
axis square;
Or write each command on separate lines (like you do later in the code). Note that you will see other errors as your code tries to reference the variable red which is not defined and would be cleared by the clear all.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!