How can I make plot lines different colors when solving PDE?

I am attempring to plot solutions of a system of PDE's, my code gives me the solutions I am looking for but I require lines from u and v to be corresponding colors. I have attempted this in the following for loop but I keep getting an error saying
Error using plot
Vectors must be the same length.
Error in untitled5 (line 12)
plot(x, u(:,1),'--',line_color(k))
How can I ensure that each line of my plot is a different color and are corresponding?
m = 0;
x = -100:0.1:100;
t=0:5:40;
sol=pdepe(m,@eq2,@eq2IC,@eq2BC,x,t);
u=sol(:,:,1);
v=sol(:,:,2);
line_color = ['r' 'g' 'b' 'c' 'm' 'y' 'k' '#7E2F8E' '#A2142F' ];
for k = 1:length(line_color)
plot(x, u(:,1),'--',line_color(k))
end
hold on
for k = 1:length(line_color)
plot(x, v(:,1),'-', line_color(k))
end
grid("on")
title('Density of population at x for given t')
ylabel('Density of population (u(x,t)/v(x,t)' )
xlabel('Location (x)')
legend('u(x,0)', 'u(x,5)', 'u(x,10)','u(x,15)', 'u(x,20)', 'u(x,25)', 'u(x,30)', 'u(x,35)', 'u(x,40)','v(x,0)', 'v(x,5)', 'v(x,10)','v(x,15)', 'v(x,20)', 'v(x,25)', 'v(x,30)', 'v(x,35)', 'v(x,40)')
lcn('bestoutside')
hold off
function [c,f,s] = eq2(x,t,u,dudx)
D = 2;
r = 1.5;
a = 0.6;
b = 0.7;
c=[1; 1];
f = [dudx(1);
D*dudx(2)];
s=[u(1)*(1-u(1)-a*u(2));
r*u(2)*(1-u(2)-b*u(1))];
end
function u0 = eq2IC(x)
if (x>=-1 && x<=1)
u0 = [1;0.2];
else
u0 = [1;0];
end
end
function [pl,ql,pr,qr] = eq2BC(xl,ul,xr,ur,t) % Boundary Conditions
pl = [0;0];
ql = [1;1];
pr = [0;0];
qr = [1;1];
end

 Réponse acceptée

There are problems referenceing ‘u’ and ‘v’ since they are both (9x2001) matrices, so to be comkpatible with ‘x’ address the rows, not the columns. Create ‘line_color’ as a cell array (and then use cell array indexing to refer to its elements), since the square brackets [] are a concatenation operator in MATLAB, making the last two elements indecipherable otherwise. There are still a few problems, however this addresses the requested problems.
m = 0;
x = -100:0.1:100;
t=0:5:40;
sol=pdepe(m,@eq2,@eq2IC,@eq2BC,x,t);
u=sol(:,:,1);
v=sol(:,:,2);
line_color = {'r' 'g' 'b' 'c' 'm' 'y' 'k' '#7E2F8E' '#A2142F' };
for k = 1:length(line_color)
plot(x, u(1,:),'--','Color',line_color{k})
end
hold on
for k = 1:length(line_color)
plot(x, v(1,:),'-', 'Color',line_color{k})
end
grid("on")
title('Density of population at x for given t')
ylabel('Density of population (u(x,t)/v(x,t)' )
xlabel('Location (x)')
hl = legend('u(x,0)', 'u(x,5)', 'u(x,10)','u(x,15)', 'u(x,20)', 'u(x,25)', 'u(x,30)', 'u(x,35)', 'u(x,40)','v(x,0)', 'v(x,5)', 'v(x,10)','v(x,15)', 'v(x,20)', 'v(x,25)', 'v(x,30)', 'v(x,35)', 'v(x,40)');
Warning: Ignoring extra legend entries.
hl.Location = 'bestoutside';
hold off
function [c,f,s] = eq2(x,t,u,dudx)
D = 2;
r = 1.5;
a = 0.6;
b = 0.7;
c=[1; 1];
f = [dudx(1);
D*dudx(2)];
s=[u(1)*(1-u(1)-a*u(2));
r*u(2)*(1-u(2)-b*u(1))];
end
function u0 = eq2IC(x)
if (x>=-1 && x<=1)
u0 = [1;0.2];
else
u0 = [1;0];
end
end
function [pl,ql,pr,qr] = eq2BC(xl,ul,xr,ur,t) % Boundary Conditions
pl = [0;0];
ql = [1;1];
pr = [0;0];
qr = [1;1];
end
.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by