Indexing and for loop
Show older comments
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
Rik
on 15 Nov 2018
You should have posted this as a comment under your previous question, now the conversation might be split up between this question and your previous one.
We don't have the ODE4550kg function, so we can't test your code. I can't tell at a glance where you have a problem, especially since you appear to have followed your professors advice on how to fix your code.
If you want help here, you should make sure that it is clear to us what your exact question/error is.
David Geistlinger
on 15 Nov 2018
Bob Thompson
on 15 Nov 2018
Indexing the double prime is fairly simple. It just means that you need to turn the double prime variable into an array, rather than a single value.
if
xx2_double_prime(x,a) = ... % Indexes so that each x2 loop is a row, and each x2_prime loop is a column
end
You will need to remember to treat this variable as an array thoughout the rest of your code, so that you only call single values, rather than the entire array (unless that's what you want/need).
David Geistlinger
on 15 Nov 2018
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
on 15 Nov 2018
Answers (0)
Categories
Find more on Programming 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!