Indexing and for loop

4 views (last 30 days)
David Geistlinger
David Geistlinger on 15 Nov 2018
Commented: David Geistlinger on 15 Nov 2018
The code listed below is part of the ode45 for a second order problem. I am not getting the right solution with it and my teacher gave me the following comments to fix my problem, however I do not understand what she is trying to say. The below code might be the wrong approach please help. Here is what professor suggested:
"The for loop should be added to loop this code block:
if xx2 >= 0 & xx2 <= 8
xx2_double_prime = (we-dr)./ma;
else
xx2_double_prime= (we-dr-bu-re)./ma;
end
However, the computation of the variable used for we, dr, bu, and re are all based on the equations above this code block, so more code needs to be moved into this for loop.
% add a for loop from here
dr= co.*(xx2_prime).^2;
bu= 100.*(xx2-8);
re= 3.*(xx2_prime);
if xx2 >= 0 & xx2 <= 8
xx2_double_prime = (we-dr)./ma;
else
xx2_double_prime= (we-dr-bu-re)./ma;
end
% for loop ends here
In addition to use the xx2 index to check one element at a time from the xx2 array, please also use each index of xx2_prime to compute dr and re. and use xx2 index to compute bu.
The xx2_double_prime in the for loop also needs an index to save one element at a time."
Please help or point me in the right direction of what I am doing wrong. I have looked up in our matlab book many videos on youtube and many questions and answer on here with no luck.
[t, u]=ode45(@ODE4550kg,[0:0.1:300],[0,0]);
x2_prime = u(:,1); % all rows and column 1 of u
x2 = u(:,2); % all rows and column 2 of u
m = 50;
g = 9.81;
w= m*g;
c= w/55^2;
for x=1:length(x2)
for a=1:length(x2_prime)
d= c.*(x2_prime(a)).^2;
b= 110.*(x2(x)-8);
r= 3.*(x2_prime(a));
if x2(x) >= 0 && x2(x) <= 8
x2_double_prime = (w-d)./m;
else
x2_double_prime= (w-d-b-r)./m;
end
end
end
  6 Comments
Bob Thompson
Bob Thompson on 15 Nov 2018
If you're trying to plot the information keep in mind that some 3D plots may require you to reverse the indexing of xx2_double_prime in order to plot properly with respect to x2 and x2_prime.
David Geistlinger
David Geistlinger on 15 Nov 2018
no just plotting each one with respect to t n a subplot
i

Sign in to comment.

Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!