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 件のコメント

Rik
Rik 2018 年 11 月 15 日
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
David Geistlinger 2018 年 11 月 15 日
i think its the last part of here suggestion of the xx2 double prime needs to be indexed as well to save one element at a time not understanding that. i can post the ODE45 function if needed.
Bob Thompson
Bob Thompson 2018 年 11 月 15 日
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
David Geistlinger 2018 年 11 月 15 日
im just trying to add them to a plot. where x2 would be position ,x2 prime is velocity and xx2 double prime is acceleration. i will see if this works thank you
Bob Thompson
Bob Thompson 2018 年 11 月 15 日
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 2018 年 11 月 15 日
no just plotting each one with respect to t n a subplot
i

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

タグ

質問済み:

2018 年 11 月 15 日

コメント済み:

2018 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by