how to call odefunction from classdef

1 visualizzazione (ultimi 30 giorni)
I want to call odefunction from classdef, but I don't know the way.
classdef plactice
properties
L = 1;
m = 1;
g = 9.8;
Tspan = linspace(0,2,20);
theta_ic = [0;0];
b = 0.01
u = randi([10 25],1,1);
end
methods
function [dtheta_dt] = ode_function(t, theta,g,L,u,b)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt =-(g/L)*(theta1)-b*(theta2)-u;
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
end
end
The class may be written incorrectly.Is it written correctly?
please someone tell me.
Check the call to the function'ode_function' for incorrect argument data types or missing arguments.

Risposta accettata

Steven Lord
Steven Lord il 31 Lug 2021
With the way you've written this at least one of the inputs to ode_function must be a plactice object. ode45 won't call your function with either t or y a plactice object.
In addition, I suspect you expect that because the ode_function method has additional input arguments beyond the second required one whose names match the properties of your object that MATLAB will "automatically" pass those property values into your ode_function. That is not the case.
You can do this approach using Constant properties and a Static method. You would call it like:
sol = ode45(@plactice.ode_function, plactice.Tspan, plactice.theta_ic)
Here is the updated definition of plactice.
classdef plactice
properties(Constant)
L = 1;
m = 1;
g = 9.8;
Tspan = linspace(0,2,20);
theta_ic = [0;0];
b = 0.01
u = randi([10 25],1,1);
end
methods(Static)
function [dtheta_dt] = ode_function(t, theta)
% Define local variables to make the expression for dtheta2_dt
% shorter
g = plactice.g;
L = plactice.L;
u = plactice.u;
b = plactice.b;
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt =-(g/L)*(theta1)-b*(theta2)-u;
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
end
end
It would be possible to do this with a non-Static method and non-Constant properties, but you would have to pass an instance of your object into ode_function as an additional parameter. The ode45 documentation page includes a link to a page describing a few techniques for passing additional parameters into the ode function.
  6 Commenti
Steven Lord
Steven Lord il 2 Ago 2021
Whenever you call a Static function you need to do so using the name.
q = plactice.ball_gosa()
As long as you fix the typo on the last line of ball_gosa and use element-wise multiplication (.*) instead of matrix multiplication (*) on the next to last line the call will succeed.
ryunosuke tazawa
ryunosuke tazawa il 4 Ago 2021
Thank you very much for your help. Thank you for kindly answering the questions to me who just started matlab.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by