You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Why ode45 is not working??
1 view (last 30 days)
Show older comments
Hello,
I've uploaded the two following files:
1. epas_steering_system.m is the main file, and it is the file where ode45 gives me an error,
2. epasFun.m is the function file.
My question is, does somebody know why ode45 won't work?
Thanks.
35 Comments
Torsten
on 19 Jul 2021
Edited: Torsten
on 19 Jul 2021
If the two files are not too big in size, maybe you could include them here as plain text. I have no possibility to download files at the moment.
Also including the complete error message would be of great help.
Torsten
on 19 Jul 2021
Edited: Torsten
on 19 Jul 2021
1 The definition of I from Ms must be done elementwise:
for i = 1:numel(time)
if Ms(i) >= 3.5
I(i) = ...
....
end
2 Ms(x), I(x) and Fr(x) in the list of parameters are incorrect.
Make a loop when calling the ode solver over the number of elements in the array time:
for i = 1:numel(time)
[T{i},X{i}] = ode45(@(t,x) epasFun(x,...,Ms(i),I(i),Fr(i)),...
end
Frane
on 19 Jul 2021
Edited: Frane
on 19 Jul 2021
I've changed the things you wrote (I hope I changed them properly). The code is uploaded in a text file so you can check it. But it still won't work. Maybe I've done it wrong?
Can you explain to me what does numel mean/represent in the for loop?
And one more thing, why can't I see the "I" for the given "Ms"? I can write out all the "Ms" but can't write out "I"?
Frane
on 19 Jul 2021
I got the part for Ms and I okey now as seen in the following (now it gives me the appropiate I for every Ms):
for i = 1:numel(time)
if Ms(i) >=3.5
I(i) = 21.29 * Ms(i) - 69.4;
elseif Ms(i) < 3.5 & Ms(i) >= 2
I(i) = 2.73 * Ms(i) - 4.47;
elseif Ms(i) < 2 & Ms(i) >= 0
I(i) = 0.5 * Ms(i);
elseif Ms(i) < 0 & Ms(i) >= (-2)
I(i) = 0.5 * Ms(i);
elseif Ms(i) < (-2) & Ms(i) >= (-3.5)
I(i) = 2.73 * Ms(i) + 4.47;
elseif Ms(i) < (-3.5)
I(i) = 21.29 * Ms(i) + 69.4;
end
end
But the part for the ode45 is written wrong. I just don't know what? Can you write it in the right way? :/
for i = 1:numel(time)
tspan = 0:0.1:3;
x0 = [0; 0; 0; 0; 0; 0; 0; 0; 0];
[t(i),x(i)] = ode45(@(t,x)epasFun(x,Juc,Jlc,kuc,klc,duc,dlc,rpin,mr,dm,Jm,K,Ms(i),I(i),Fr(i)), tspan, x0);
end
Torsten
on 19 Jul 2021
Edited: Torsten
on 19 Jul 2021
One error is that you must replace Fr(i) by Fr in the list of parameters handed to epasFun because Fr=1 is a scalar.
If the error persists, check the size of all variables handed to epasFun in this function. One of them must have size 3x9 instead of 1. So insert the lines
size(x)
size(Juc)
size(Jlc)
etc
at the beginning of function epasFun.
Frane
on 20 Jul 2021
Yeah it works now. If I want to plot the T and X, I can't use plot. What can I use to see the variables? Because in an example I've seen online they used plot. Here is the following code and the plot after it.
figure
plot(t,x,'LineWidth',2);
legend('x', 'v', 'theta', 'omega');
xlabel('Vrijeme');
ylabel('Stanje');
set(gcf,'color','w');
title('Odaziv svih varijabli sustava sa LQR');
Torsten
on 20 Jul 2021
I'm not completely sure how to deal with the cell arrays T and X. So to keep control, change the last part of the code to
tspan = 0:0.1:3;
x0 = zeros(9,1);
X = zeros(numel(time),numel(tspan),9);
for i = 1:numel(time)
[t,x] = ode45(...);
X(i,:,:) = x(:,:);
end
Now you can plot, e.g. all nine components of the solution for Ms(1) and I(1) :
plot (t,X(1,:,:))
Frane
on 20 Jul 2021
Edited: Frane
on 20 Jul 2021
Yeah the X is 20x3x9. Can I show all the results at the same time or one by one like this? There are different result (example for X(:,:6), x(:,:,7)) and some are 0.
Even by changin tspan I can only see 12 of them.
Frane
on 20 Jul 2021
Edited: Frane
on 20 Jul 2021
You wrote that there shoud be 20 curves because numel(time) = 20 and the time is:
time = 0.01: 0.01: 0.2;
So why 20 curves? I don't understand? Can you explain to me?
Thanks.
P.S. Do these curve respresent the outputs given in the left side of the equation in the picture below (even though there are 9 outputs)?
Torsten
on 20 Jul 2021
If you now tell me that Ms and I are interpolation curves dependent on t and that the values Ms(t) and I(t) have to be inserted in the equations when the solver has reached time t, we can start anew modifying your code.
Frane
on 20 Jul 2021
Edited: Frane
on 20 Jul 2021
Just to explain Ms and I:
Ms - represents the momentum given to the steering wheel
I - is the current given by the electromotor to help steer, depending on the momentum Ms
The part in the beggining is from the picture below it (hope this answers you question above?).
P.S. Ms, I and Fr are inputs (where Fr is given by the CarMaker; that's why it's value is 1).
for i = 1:numel(time)
if Ms(i) >=3.5
I(i) = 21.29 * Ms(i) - 69.4;
elseif Ms(i) < 3.5 & Ms(i) >= 2
I(i) = 2.73 * Ms(i) - 4.47;
elseif Ms(i) < 2 & Ms(i) >= 0
I(i) = 0.5 * Ms(i);
elseif Ms(i) < 0 & Ms(i) >= (-2)
I(i) = 0.5 * Ms(i);
elseif Ms(i) < (-2) & Ms(i) >= (-3.5)
I(i) = 2.73 * Ms(i) + 4.47;
elseif Ms(i) < (-3.5)
I(i) = 21.29 * Ms(i) + 69.4;
end
end
Frane
on 20 Jul 2021
Edited: Frane
on 20 Jul 2021
Because the driver rotates the wheel differently during a period of time, and depending on the momentum given by the driver (Ms), a current (I) is sent to help the driver to turn the wheel (so he doesn't use too much "force").
Could that be the reason otherwise I don't know. I was given by the proffesor like that.
P.S. When I change the "time" from
time = 0.01: 0.01: 0.2;
to
time = 0.01: 0.01: 0.1;
I get less curves (and the other way around).
Torsten
on 20 Jul 2021
At the moment, the equations are solved for Ms and I being fixed parameter values over the integration time. Thus for each combination for Ms and I (and there exist as many combinations as the vector "time" has elements), you get 9 solution curves over the time period defined by "tspan". I suspect that the time instants specified in "time" and "tspan " must be identical and that the values of Ms and I must be interpolated to the integration time provided by ODE45 in function epasFun. But this is not what is simulated in the actual code.
Frane
on 20 Jul 2021
Yes when "time" and "tspan" are the same I get 9 curves representing different "Ms" and "I".
I'm doing this based on an example of inverted pendulum on cart (I've uploaded the files). In lqr_pendcart the guy made the controler "u" that in this case controled the force by which the cart was being pulled or pushed. There is only one input (the force F represented by u) and four outputs given in a plot when you run the code (also you get two stepplots one before using LQR and one after). So my task was to make a controler in a similar way that they did with the inverted pendulum on cart, and I needed to get the response of the ouput variables similar like in the given example.
Frane
on 21 Jul 2021
Can I ask you one more thing? In my case i did a stepplot (code and picture is given below). I was wondering am I looking at the picture in the red rounded direction or the blue direction? I know it represents my 3 inputs but I don't know how to look at the plot?
figure
sys = ss(A, B, C, D); % ss = prostor stanja
t1 = 0:0.1:5;
stepplot(sys,t1);
set(gcf,'color','w');
xlabel('Vrijeme');
ylabel('Odziv');
hold on;
Frane
on 21 Jul 2021
So I went to my professor, he changed my code a little bit (some of the code that was in epas_steering_system is now in epasFun). He told me that I shoud do ode45 without for loop (given in the files below). But as you can see all of my variables divaricate, and they shoud converge. Do you maybe know what could be the problem? Maybe I've written some of the equations wrong in epasFun, or maybe the formula for Ms and I is wrong, or some of the parameters are wrong? But expect of the given problems could there be something else?
Answers (1)
Tesfaye Girma
on 21 Jul 2021
you can try this approach if it worked for you
f = (t,y) [ 4.86*y(3) - 4.86*10^14*y(1)*y(2);
4.86*y(3) - 4.86*10^14*y(1)*y(2);
-4.86*y(3) + 4.86*10^14*y(1)*y(2) ];
opt = odeset('maxstep',1e-13);
tspan = [0 1e-11];
y0 = [1.48e-8; 6.7608e-3; 1];
[t,y] = ode45(f,tspan,y0,opt);
subplot(311)
plot(t,y(:,1))
subplot(312)
plot(t,y(:,2))
subplot(313)
plot(t,y(:,3))
3 Comments
Ildeberto de los Santos Ruiz
on 21 Jul 2021
The @ character is missing when you define .
f = @(t,y) [...
Tesfaye Girma
on 22 Jul 2021
you are right thank you brother!!
Frane
on 22 Jul 2021
Edited: Frane
on 22 Jul 2021
Where did you get this numbers that are multiplying y(1), y(2) and y(3) in f=(t,y), and the numbers from y0?
Did you try my code and added these numbers and it worked or?
And what should y(1), y(2) and y(3) represent?
See Also
Categories
Find more on Ordinary Differential Equations 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)