something is wrong in my plotting code

i dont know what is wrong with that code first
second i want all x in one color and all y' in onther color
thx for helping me alot
syms y(t) t Y a b x(t)
[V,Subs] = odeToVectorField(diff(y, 2) == -1*(a-b*cos(2*t))*y);
M = matlabFunction(V,'Vars',{t,Y,a, b});
tspan = linspace(0, pi, 75);
av = 0:10;
bv = 0:5;
for k1 = 1:numel(av)
for k2 = 1:numel(bv)
[t,y] = ode45(@(t,Y)M(t,Y,av(k1),bv(k2)),tspan,[0 1]);
[t,x] = ode45(@(t,Y)M(t,Y,av(k1),bv(k2)),tspan,[1 0]);
yc{k1,k2,:} = y;
xc{k1,k2,:} = x;
end
end
figure
hold on
for k1 = 1:101
for k2 = 1:51
hold on
plot(t, xc{k1,k2,:}(:,1));
plot(t, yc{k1,k2,:}(:,2));
hold off
end
end
hold off
grid

回答(1 个)

syms y(t) t Y a b x(t)
[V,Subs] = odeToVectorField(diff(y, 2) == -1*(a-b*cos(2*t))*y);
M = matlabFunction(V,'Vars',{t,Y,a, b});
tspan = linspace(0, pi, 75);
av = 0:10;
bv = 0:5;
nav = numel(av);
nbv = numel(bv);
yc = cell(nav, nbv);
xc = cell(nava, nbv);
for k1 = 1:nav
for k2 = 1:nbv
[t,y] = ode45(@(t,Y)M(t,Y,av(k1),bv(k2)),tspan,[0 1]);
[t,x] = ode45(@(t,Y)M(t,Y,av(k1),bv(k2)),tspan,[1 0]);
yc{k1,k2} = y;
xc{k1,k2} = x;
end
end
figure
hold on
for k1 = nav
for k2 = nbv
hold on
plot(t, xc{k1,k2}(:,1), 'displayname', sprintf('x{%d,%d}', k1, k2));
plot(t, yc{k1,k2}(:,2), 'displayname', sprintf('y{%d,%d}', k1, k2));
hold off
end
end
hold off
grid
legend show

2 个评论

thx for help but i done some small changes to make it work but after that the colors as u see too much colors i hope that its became just 2 color one for x and one for y
syms y(t) t Y a b x(t)
[V,Subs] = odeToVectorField(diff(y, 2) == -1*(a-b*cos(2*t))*y);
M = matlabFunction(V,'Vars',{t,Y,a, b});
tspan = linspace(0, pi, 75);
av = 0:10;
bv = 0:5;
nav = numel(av);
nbv = numel(bv);
yc = cell(nav, nbv);
xc = cell(nav, nbv);
for k1 = 1:nav
for k2 = 1:nbv
[t,y] = ode45(@(t,Y)M(t,Y,av(k1),bv(k2)),tspan,[0 1]);
[t,x] = ode45(@(t,Y)M(t,Y,av(k1),bv(k2)),tspan,[1 0]);
yc{k1,k2} = y;
xc{k1,k2} = x;
end
end
figure
hold on
for k1 = 1:11
for k2 = 1:6
hold on
plot(t, xc{k1,k2}(:,1), 'displayname', sprintf('x{%d,%d}', k1, k2));
plot(t, yc{k1,k2}(:,2), 'displayname', sprintf('y{%d,%d}', k1, k2));
hold off
end
end
hold off
grid
legend show
syms y(t) t Y a b x(t)
[V,Subs] = odeToVectorField(diff(y, 2) == -1*(a-b*cos(2*t))*y);
M = matlabFunction(V,'Vars',{t,Y,a, b});
tspan = linspace(0, pi, 75);
av = 0:10;
bv = 0:5;
nav = numel(av);
nbv = numel(bv);
yc = cell(nav, nbv);
xc = cell(nava, nbv);
for k1 = 1:nav
for k2 = 1:nbv
[t,y] = ode45(@(t,Y)M(t,Y,av(k1),bv(k2)),tspan,[0 1]);
[t,x] = ode45(@(t,Y)M(t,Y,av(k1),bv(k2)),tspan,[1 0]);
yc{k1,k2} = y;
xc{k1,k2} = x;
end
end
figure
hold on
xcolor = 'k';
ycolor = 'b';
for k1 = 1 : nav
for k2 = 1 : nbv
hold on
plot(t, xc{k1,k2}(:,1), xcolor, 'displayname', sprintf('x{%d,%d}', k1, k2));
plot(t, yc{k1,k2}(:,2), ycolor, 'displayname', sprintf('y{%d,%d}', k1, k2));
hold off
end
end
hold off
grid
legend show

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by