Finding corresponding x value for second graph

3 views (last 30 days)
Oskar
Oskar on 19 Dec 2017
Answered: Kelly Kearney on 19 Dec 2017
I made 2 graphs and need help finding the corresponding 'x' value for the second graph.
time_period = [0 181324/20000];
initial = [0, 0];
[t,y]=ode45(@myode45function, time_period, initial); %calls the ode45 function to plot y against t
plot(t,y(:,1)),title('Graph of y against t') %plots a graph of y against t
xlabel('t')
ylabel('y')
ymax=max(y(:,1));
disp('The maximum value of y is ')
disp(ymax);
position = find(y==ymax); %finds the position where y is at its maximum
Tmin=t(position); %and finds the corresponding value of t at this position
disp('The corresponding value of t when y is maximum is:')
disp(Tmin) %displays the value of t
figure; %new graph
plot(t,y(:,2)),title('Graph of dy/dt against t') %plots a graph of dy/dt against t with title
xlabel('t')
ylabel('dy/dt')
ymax1=max(y(:,2));
disp('The maximum value of dy/dt is: ')
disp(ymax1);
This is the function
function myoutput = myode45function(t,y)
myoutput = [y(2); 10*cos(t) - 12*y(2)-35*y(1)];
end
I tried doing this at the end of the code:
position2 = find(y==ymax1);
Tmin2=t(position2);
disp('The corresponding value of t when dy/dx is maximum is:')
disp(Tmin2)
end but i get an error saying "Index exceeds matrix dimensions. Error in assignment_five (line 22) Tmin2=t(position2);"

Answers (1)

Kelly Kearney
Kelly Kearney on 19 Dec 2017
Best guess, seeing that I can't see what your myode45function returns... It looks like your y matrix has at least 2 columns. The t output of an ode solver is usually (always?) a vector, though. So if your find query is finding max values anywhere beyond the first column of y, the returned index will be greater than the length of t.

Categories

Find more on 2-D and 3-D Plots 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!