i want to plot a certain number of a function

1 view (last 30 days)
Saladin Hossam
Saladin Hossam on 31 Jul 2020
Edited: Rik on 31 Jul 2020
i want to find y(pi) so for every k1 and k2 then if y(pi) <1 plot it as a green point and if y(pi) >1 plot it as a blue pont
so i want to plot a 100 point of all value of y(pi) if k1= 0:10 and k2=0:10
syms y(t) t Y a b
[V,Subs] = odeToVectorField(diff(y, 2) == -1*(a-b*cos(2*t))*y);
M = matlabFunction(V,'Vars',{t,Y,a, b});
tspan = linspace(0, 2*pi, 75);
av = 0:10;
bv = 0:10;
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]);
yc{k1,k2,:} = y;
end
end
figure
hold on
for k1 = 1:11
for k2 = 1:11
plot(t,yc{k1,k2,:})
end
end
hold off
grid
  2 Comments
Rik
Rik on 31 Jul 2020
What is your goal? What exactly do you want to plot? What should be on each axis?
Saladin Hossam
Saladin Hossam on 31 Jul 2020
Edited: Rik on 31 Jul 2020
the axis are k1 and k2
i want to plot this function at t=pi means y(pi)

Sign in to comment.

Answers (1)

Rik
Rik on 31 Jul 2020
Edited: Rik on 31 Jul 2020
A surface (or plot3) would maybe make more sense, but here is what you asked for. Ideally you would use an interpollation inside the loop (e.g. interp), but I just looked up the t that has the smallest difference with pi, which should work, as tspan is odd and should contain pi.
syms y(t) t Y a b
[V,Subs] = odeToVectorField(diff(y, 2) == -1*(a-b*cos(2*t))*y);
M = matlabFunction(V,'Vars',{t,Y,a, b});
tspan = linspace(0, 2*pi, 75);
av = 0:10;
bv = 0:10;
y_pi=zeros(numel(av),numel(bv));
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]);
%store y for t==pi (interpolation would be better, but is left as an excersize for the reader)
[~,pi_]=min(abs(t-pi));
y_pi(k1,k2) = y(pi_);
end
end
k1=1:numel(av);
k2=1:numel(bv);
[X,Y]=ndgrid(k1,k2);
Z=y_pi;
figure(1),clf(1)
%plot green points
L=Z<1;
plot(X(L),Y(L),'.g'),hold on
%plot blue points
%(note: this is y(pi)>=1, not y(pi)>1)
plot(X(~L),Y(~L),'.b'),hold off
xlabel('k1'),ylabel('k2')
Here is the code for a surface plot as well. Increasing the granularity of av and bv (e.g. with av=linspace(0,10,30);bv=linspace(0,10,30);) should get you a better resolution for this plot.
figure(2),clf(2)
surf(Z)
caxis([1-eps 1+eps])%center the coloraxis around 1
colormap([0 0 1; 0 1 0])%make low values green and high values blue
  2 Comments
Saladin Hossam
Saladin Hossam on 31 Jul 2020
Edited: Rik on 31 Jul 2020
%(note: this is y(pi)>=1, not y(pi)>1)
i want it for y(pi)>1 not y(pi)>=1 what should i do
and sir i dont understand that point
[~,pi_]=min(abs(t-pi));
Rik
Rik on 31 Jul 2020
Just 3 lines above it you can see how to do it. What do you think? Guessing is fine, as long as you test it and try to understand why something does or doesn't work.
And what part don't you understand about that line? The min? The abs? The subtraction? Why I'm getting the second output? What is happening conceptually on that line?
Did you read the documentation for those functions?

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!