Change value of variable at each time step - Matlab ODE function
up vote
1
down vote
favorite
I have the following system of differential equations to simulate in a .m
file:
function dx = odefun(t,x)
% display(x(1));
% y = 5;
dx = [x(2); - y - 3*x(2) - 2*x(1)];
end
I am simulating the system running another .m
file with the code below:
[t,x] = ode45(@odefun, [0 10], [0;0]);
% display(x(:,1));
plot(t,x(:,1));
My problem is that I want the value of y
parameter, which happens to be the output of my system, to change in each time step while the ode(...)
function is executing. I tried by sending another argument like this:
[t,x] = ode45(@odefun, [0 10], [0;0], [some_elements]);
function dx = odefun(t,x,y)
but I get the error: Not enough input arguments.
Truth is that I want y
parameter to take one value at each time step from a vector with a hundred elements. Any help would be greatly appreciated.
matlab ode differential-equations
add a comment |
up vote
1
down vote
favorite
I have the following system of differential equations to simulate in a .m
file:
function dx = odefun(t,x)
% display(x(1));
% y = 5;
dx = [x(2); - y - 3*x(2) - 2*x(1)];
end
I am simulating the system running another .m
file with the code below:
[t,x] = ode45(@odefun, [0 10], [0;0]);
% display(x(:,1));
plot(t,x(:,1));
My problem is that I want the value of y
parameter, which happens to be the output of my system, to change in each time step while the ode(...)
function is executing. I tried by sending another argument like this:
[t,x] = ode45(@odefun, [0 10], [0;0], [some_elements]);
function dx = odefun(t,x,y)
but I get the error: Not enough input arguments.
Truth is that I want y
parameter to take one value at each time step from a vector with a hundred elements. Any help would be greatly appreciated.
matlab ode differential-equations
You can not "change value of variable at each time step" as you do not know what time steps the function is called at. Ify
is indeed an externally provided function given by a function table, make sure to set the maximum time step smaller than the sampling rate of it. In general read aboutodeset
for setting options like parameters of the ODE function.
– LutzL
Nov 11 at 8:29
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the following system of differential equations to simulate in a .m
file:
function dx = odefun(t,x)
% display(x(1));
% y = 5;
dx = [x(2); - y - 3*x(2) - 2*x(1)];
end
I am simulating the system running another .m
file with the code below:
[t,x] = ode45(@odefun, [0 10], [0;0]);
% display(x(:,1));
plot(t,x(:,1));
My problem is that I want the value of y
parameter, which happens to be the output of my system, to change in each time step while the ode(...)
function is executing. I tried by sending another argument like this:
[t,x] = ode45(@odefun, [0 10], [0;0], [some_elements]);
function dx = odefun(t,x,y)
but I get the error: Not enough input arguments.
Truth is that I want y
parameter to take one value at each time step from a vector with a hundred elements. Any help would be greatly appreciated.
matlab ode differential-equations
I have the following system of differential equations to simulate in a .m
file:
function dx = odefun(t,x)
% display(x(1));
% y = 5;
dx = [x(2); - y - 3*x(2) - 2*x(1)];
end
I am simulating the system running another .m
file with the code below:
[t,x] = ode45(@odefun, [0 10], [0;0]);
% display(x(:,1));
plot(t,x(:,1));
My problem is that I want the value of y
parameter, which happens to be the output of my system, to change in each time step while the ode(...)
function is executing. I tried by sending another argument like this:
[t,x] = ode45(@odefun, [0 10], [0;0], [some_elements]);
function dx = odefun(t,x,y)
but I get the error: Not enough input arguments.
Truth is that I want y
parameter to take one value at each time step from a vector with a hundred elements. Any help would be greatly appreciated.
matlab ode differential-equations
matlab ode differential-equations
asked Nov 10 at 21:57
Teo Protoulis
135
135
You can not "change value of variable at each time step" as you do not know what time steps the function is called at. Ify
is indeed an externally provided function given by a function table, make sure to set the maximum time step smaller than the sampling rate of it. In general read aboutodeset
for setting options like parameters of the ODE function.
– LutzL
Nov 11 at 8:29
add a comment |
You can not "change value of variable at each time step" as you do not know what time steps the function is called at. Ify
is indeed an externally provided function given by a function table, make sure to set the maximum time step smaller than the sampling rate of it. In general read aboutodeset
for setting options like parameters of the ODE function.
– LutzL
Nov 11 at 8:29
You can not "change value of variable at each time step" as you do not know what time steps the function is called at. If
y
is indeed an externally provided function given by a function table, make sure to set the maximum time step smaller than the sampling rate of it. In general read about odeset
for setting options like parameters of the ODE function.– LutzL
Nov 11 at 8:29
You can not "change value of variable at each time step" as you do not know what time steps the function is called at. If
y
is indeed an externally provided function given by a function table, make sure to set the maximum time step smaller than the sampling rate of it. In general read about odeset
for setting options like parameters of the ODE function.– LutzL
Nov 11 at 8:29
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Not tested on Matlab but just to help and mainly wrapped up from https://fr.mathworks.com/help/matlab/ref/ode45.html#bu00_4l_sep_shared-options ODE with Time-Dependent Terms
function dx = odefun(t,x,y,yt)
f = interp1(yt,y,t);
dx = [x(2); - f - 3*x(2) - 2*x(1)];
end
%define your array (or even function) y(yt)
yt = 0.0:0.1:10.0;
y = array; % array being your array of input values. (should contain the corresponding value for 0.0:0.1:10.0
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), [0.0 10.0], [0;0]);
or try the following if you want specific time steps included in your result :
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), yt, [0;0]);
You can also do, to be sure to have your solution corresponding to 0.0:0.1:10.0,
x100 = interp1(t,x,yt);
If my y vector is a 1x100 vector, shouldn't the returned value of the ode function, the x vector, also be a 1x100 vector ? Like I have 100 input values and I get 100 output values.
– Teo Protoulis
Nov 11 at 0:26
There is no reason for y to be the same size (that is why we use interp1). y should only de defined on the temporal slice solved by ode45.
– PilouPili
Nov 11 at 8:31
The fact is that I have an input of a system with 100 values and I want to take the output of the system using the differential equation for each input. Values of the input are measured each 0.1sec in a range of 0 to 10 seconds.
– Teo Protoulis
Nov 11 at 12:38
then you specify yt = 0:0.1:10 and y is then value of your input system. Since your already specified tspan=[0 10] everything should then run ok. I have edited the answer
– PilouPili
Nov 11 at 13:16
Still having a 1x100 vector with input values and getting back a 41x2 matrix with output values instead of my desired 100x2. Your answer works fine but maybe I am not stating things clear. It is like this: enter an input value - take one output and so on for 100 input values - 100 output values.
– Teo Protoulis
Nov 11 at 13:38
|
show 2 more comments
up vote
0
down vote
For those interested I post here the way I finally managed to get what I want. I have a 1x100 vector representing the input to a system and I want to take back a 1x100 vector consisting of the values of the output of my system. I wanted one value as output for each value of my input.
global f
t = 0.1:0.1:10.0;
f = @create_signal;
[t,x] = ode45(@odefun, t, [0;0]);
and the odefun
function is like this:
function dx = odefun(t,x)
global f
m = 15;
b = 0.2;
k = 2;
u = f(t);
dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];
end
and lastly the function that creates the values of the input:
function u = create_signal(t)
u = 5*sin(t) + 10.5;
end
I applied the create function handle feature of MATLAB. Here is the link of mathworks help: https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Not tested on Matlab but just to help and mainly wrapped up from https://fr.mathworks.com/help/matlab/ref/ode45.html#bu00_4l_sep_shared-options ODE with Time-Dependent Terms
function dx = odefun(t,x,y,yt)
f = interp1(yt,y,t);
dx = [x(2); - f - 3*x(2) - 2*x(1)];
end
%define your array (or even function) y(yt)
yt = 0.0:0.1:10.0;
y = array; % array being your array of input values. (should contain the corresponding value for 0.0:0.1:10.0
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), [0.0 10.0], [0;0]);
or try the following if you want specific time steps included in your result :
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), yt, [0;0]);
You can also do, to be sure to have your solution corresponding to 0.0:0.1:10.0,
x100 = interp1(t,x,yt);
If my y vector is a 1x100 vector, shouldn't the returned value of the ode function, the x vector, also be a 1x100 vector ? Like I have 100 input values and I get 100 output values.
– Teo Protoulis
Nov 11 at 0:26
There is no reason for y to be the same size (that is why we use interp1). y should only de defined on the temporal slice solved by ode45.
– PilouPili
Nov 11 at 8:31
The fact is that I have an input of a system with 100 values and I want to take the output of the system using the differential equation for each input. Values of the input are measured each 0.1sec in a range of 0 to 10 seconds.
– Teo Protoulis
Nov 11 at 12:38
then you specify yt = 0:0.1:10 and y is then value of your input system. Since your already specified tspan=[0 10] everything should then run ok. I have edited the answer
– PilouPili
Nov 11 at 13:16
Still having a 1x100 vector with input values and getting back a 41x2 matrix with output values instead of my desired 100x2. Your answer works fine but maybe I am not stating things clear. It is like this: enter an input value - take one output and so on for 100 input values - 100 output values.
– Teo Protoulis
Nov 11 at 13:38
|
show 2 more comments
up vote
0
down vote
accepted
Not tested on Matlab but just to help and mainly wrapped up from https://fr.mathworks.com/help/matlab/ref/ode45.html#bu00_4l_sep_shared-options ODE with Time-Dependent Terms
function dx = odefun(t,x,y,yt)
f = interp1(yt,y,t);
dx = [x(2); - f - 3*x(2) - 2*x(1)];
end
%define your array (or even function) y(yt)
yt = 0.0:0.1:10.0;
y = array; % array being your array of input values. (should contain the corresponding value for 0.0:0.1:10.0
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), [0.0 10.0], [0;0]);
or try the following if you want specific time steps included in your result :
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), yt, [0;0]);
You can also do, to be sure to have your solution corresponding to 0.0:0.1:10.0,
x100 = interp1(t,x,yt);
If my y vector is a 1x100 vector, shouldn't the returned value of the ode function, the x vector, also be a 1x100 vector ? Like I have 100 input values and I get 100 output values.
– Teo Protoulis
Nov 11 at 0:26
There is no reason for y to be the same size (that is why we use interp1). y should only de defined on the temporal slice solved by ode45.
– PilouPili
Nov 11 at 8:31
The fact is that I have an input of a system with 100 values and I want to take the output of the system using the differential equation for each input. Values of the input are measured each 0.1sec in a range of 0 to 10 seconds.
– Teo Protoulis
Nov 11 at 12:38
then you specify yt = 0:0.1:10 and y is then value of your input system. Since your already specified tspan=[0 10] everything should then run ok. I have edited the answer
– PilouPili
Nov 11 at 13:16
Still having a 1x100 vector with input values and getting back a 41x2 matrix with output values instead of my desired 100x2. Your answer works fine but maybe I am not stating things clear. It is like this: enter an input value - take one output and so on for 100 input values - 100 output values.
– Teo Protoulis
Nov 11 at 13:38
|
show 2 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Not tested on Matlab but just to help and mainly wrapped up from https://fr.mathworks.com/help/matlab/ref/ode45.html#bu00_4l_sep_shared-options ODE with Time-Dependent Terms
function dx = odefun(t,x,y,yt)
f = interp1(yt,y,t);
dx = [x(2); - f - 3*x(2) - 2*x(1)];
end
%define your array (or even function) y(yt)
yt = 0.0:0.1:10.0;
y = array; % array being your array of input values. (should contain the corresponding value for 0.0:0.1:10.0
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), [0.0 10.0], [0;0]);
or try the following if you want specific time steps included in your result :
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), yt, [0;0]);
You can also do, to be sure to have your solution corresponding to 0.0:0.1:10.0,
x100 = interp1(t,x,yt);
Not tested on Matlab but just to help and mainly wrapped up from https://fr.mathworks.com/help/matlab/ref/ode45.html#bu00_4l_sep_shared-options ODE with Time-Dependent Terms
function dx = odefun(t,x,y,yt)
f = interp1(yt,y,t);
dx = [x(2); - f - 3*x(2) - 2*x(1)];
end
%define your array (or even function) y(yt)
yt = 0.0:0.1:10.0;
y = array; % array being your array of input values. (should contain the corresponding value for 0.0:0.1:10.0
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), [0.0 10.0], [0;0]);
or try the following if you want specific time steps included in your result :
[t,x] = ode45(@(t,x) odefun(t,x,y,yt), yt, [0;0]);
You can also do, to be sure to have your solution corresponding to 0.0:0.1:10.0,
x100 = interp1(t,x,yt);
edited Nov 11 at 14:49
answered Nov 10 at 23:00
PilouPili
1,5891719
1,5891719
If my y vector is a 1x100 vector, shouldn't the returned value of the ode function, the x vector, also be a 1x100 vector ? Like I have 100 input values and I get 100 output values.
– Teo Protoulis
Nov 11 at 0:26
There is no reason for y to be the same size (that is why we use interp1). y should only de defined on the temporal slice solved by ode45.
– PilouPili
Nov 11 at 8:31
The fact is that I have an input of a system with 100 values and I want to take the output of the system using the differential equation for each input. Values of the input are measured each 0.1sec in a range of 0 to 10 seconds.
– Teo Protoulis
Nov 11 at 12:38
then you specify yt = 0:0.1:10 and y is then value of your input system. Since your already specified tspan=[0 10] everything should then run ok. I have edited the answer
– PilouPili
Nov 11 at 13:16
Still having a 1x100 vector with input values and getting back a 41x2 matrix with output values instead of my desired 100x2. Your answer works fine but maybe I am not stating things clear. It is like this: enter an input value - take one output and so on for 100 input values - 100 output values.
– Teo Protoulis
Nov 11 at 13:38
|
show 2 more comments
If my y vector is a 1x100 vector, shouldn't the returned value of the ode function, the x vector, also be a 1x100 vector ? Like I have 100 input values and I get 100 output values.
– Teo Protoulis
Nov 11 at 0:26
There is no reason for y to be the same size (that is why we use interp1). y should only de defined on the temporal slice solved by ode45.
– PilouPili
Nov 11 at 8:31
The fact is that I have an input of a system with 100 values and I want to take the output of the system using the differential equation for each input. Values of the input are measured each 0.1sec in a range of 0 to 10 seconds.
– Teo Protoulis
Nov 11 at 12:38
then you specify yt = 0:0.1:10 and y is then value of your input system. Since your already specified tspan=[0 10] everything should then run ok. I have edited the answer
– PilouPili
Nov 11 at 13:16
Still having a 1x100 vector with input values and getting back a 41x2 matrix with output values instead of my desired 100x2. Your answer works fine but maybe I am not stating things clear. It is like this: enter an input value - take one output and so on for 100 input values - 100 output values.
– Teo Protoulis
Nov 11 at 13:38
If my y vector is a 1x100 vector, shouldn't the returned value of the ode function, the x vector, also be a 1x100 vector ? Like I have 100 input values and I get 100 output values.
– Teo Protoulis
Nov 11 at 0:26
If my y vector is a 1x100 vector, shouldn't the returned value of the ode function, the x vector, also be a 1x100 vector ? Like I have 100 input values and I get 100 output values.
– Teo Protoulis
Nov 11 at 0:26
There is no reason for y to be the same size (that is why we use interp1). y should only de defined on the temporal slice solved by ode45.
– PilouPili
Nov 11 at 8:31
There is no reason for y to be the same size (that is why we use interp1). y should only de defined on the temporal slice solved by ode45.
– PilouPili
Nov 11 at 8:31
The fact is that I have an input of a system with 100 values and I want to take the output of the system using the differential equation for each input. Values of the input are measured each 0.1sec in a range of 0 to 10 seconds.
– Teo Protoulis
Nov 11 at 12:38
The fact is that I have an input of a system with 100 values and I want to take the output of the system using the differential equation for each input. Values of the input are measured each 0.1sec in a range of 0 to 10 seconds.
– Teo Protoulis
Nov 11 at 12:38
then you specify yt = 0:0.1:10 and y is then value of your input system. Since your already specified tspan=[0 10] everything should then run ok. I have edited the answer
– PilouPili
Nov 11 at 13:16
then you specify yt = 0:0.1:10 and y is then value of your input system. Since your already specified tspan=[0 10] everything should then run ok. I have edited the answer
– PilouPili
Nov 11 at 13:16
Still having a 1x100 vector with input values and getting back a 41x2 matrix with output values instead of my desired 100x2. Your answer works fine but maybe I am not stating things clear. It is like this: enter an input value - take one output and so on for 100 input values - 100 output values.
– Teo Protoulis
Nov 11 at 13:38
Still having a 1x100 vector with input values and getting back a 41x2 matrix with output values instead of my desired 100x2. Your answer works fine but maybe I am not stating things clear. It is like this: enter an input value - take one output and so on for 100 input values - 100 output values.
– Teo Protoulis
Nov 11 at 13:38
|
show 2 more comments
up vote
0
down vote
For those interested I post here the way I finally managed to get what I want. I have a 1x100 vector representing the input to a system and I want to take back a 1x100 vector consisting of the values of the output of my system. I wanted one value as output for each value of my input.
global f
t = 0.1:0.1:10.0;
f = @create_signal;
[t,x] = ode45(@odefun, t, [0;0]);
and the odefun
function is like this:
function dx = odefun(t,x)
global f
m = 15;
b = 0.2;
k = 2;
u = f(t);
dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];
end
and lastly the function that creates the values of the input:
function u = create_signal(t)
u = 5*sin(t) + 10.5;
end
I applied the create function handle feature of MATLAB. Here is the link of mathworks help: https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
add a comment |
up vote
0
down vote
For those interested I post here the way I finally managed to get what I want. I have a 1x100 vector representing the input to a system and I want to take back a 1x100 vector consisting of the values of the output of my system. I wanted one value as output for each value of my input.
global f
t = 0.1:0.1:10.0;
f = @create_signal;
[t,x] = ode45(@odefun, t, [0;0]);
and the odefun
function is like this:
function dx = odefun(t,x)
global f
m = 15;
b = 0.2;
k = 2;
u = f(t);
dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];
end
and lastly the function that creates the values of the input:
function u = create_signal(t)
u = 5*sin(t) + 10.5;
end
I applied the create function handle feature of MATLAB. Here is the link of mathworks help: https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
add a comment |
up vote
0
down vote
up vote
0
down vote
For those interested I post here the way I finally managed to get what I want. I have a 1x100 vector representing the input to a system and I want to take back a 1x100 vector consisting of the values of the output of my system. I wanted one value as output for each value of my input.
global f
t = 0.1:0.1:10.0;
f = @create_signal;
[t,x] = ode45(@odefun, t, [0;0]);
and the odefun
function is like this:
function dx = odefun(t,x)
global f
m = 15;
b = 0.2;
k = 2;
u = f(t);
dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];
end
and lastly the function that creates the values of the input:
function u = create_signal(t)
u = 5*sin(t) + 10.5;
end
I applied the create function handle feature of MATLAB. Here is the link of mathworks help: https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
For those interested I post here the way I finally managed to get what I want. I have a 1x100 vector representing the input to a system and I want to take back a 1x100 vector consisting of the values of the output of my system. I wanted one value as output for each value of my input.
global f
t = 0.1:0.1:10.0;
f = @create_signal;
[t,x] = ode45(@odefun, t, [0;0]);
and the odefun
function is like this:
function dx = odefun(t,x)
global f
m = 15;
b = 0.2;
k = 2;
u = f(t);
dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];
end
and lastly the function that creates the values of the input:
function u = create_signal(t)
u = 5*sin(t) + 10.5;
end
I applied the create function handle feature of MATLAB. Here is the link of mathworks help: https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
answered Nov 12 at 19:09
Teo Protoulis
135
135
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243808%2fchange-value-of-variable-at-each-time-step-matlab-ode-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can not "change value of variable at each time step" as you do not know what time steps the function is called at. If
y
is indeed an externally provided function given by a function table, make sure to set the maximum time step smaller than the sampling rate of it. In general read aboutodeset
for setting options like parameters of the ODE function.– LutzL
Nov 11 at 8:29