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.

 Réponse acceptée

Steven Lord
Steven Lord le 31 Juil 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 commentaires

Thanks to you, I was able to understand to some extent. However, there is something I would like to know a little more.
What should I do if I define and call the solution derived in ode45 by another function in the class?
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);
ball_target = 2.5;
end
methods(Static)
function [dtheta_dt] = ode_function(t, theta)
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
function ball_gosa = ball_function()
ball_target = plactice.ball_target;
% ↓error happend here [t. theta]...
[t, theta] = ode45(@(t,theta) ode_function(t,theta),plactice.Tspan,plactice.theta_ic);
w = theta(:,2);
theta = theta(:,1);
ball_x = sin(theta(:,1));
ball_y = -cos(theta(:,1));
ball_time = sqrt(2*abs(ball_y)/9.8);
ball_reach = ball_x +abs(w)*ball_time; % No variables assigned
ball_gosa = ball_rearch-ball_target;
Because ode_function is a Static method, you need to call it using the name of the class.
[t, theta] = ode45(@(t,theta) plactice.ode_function(t,theta),...
plactice.Tspan,plactice.theta_ic);
ryunosuke tazawa
ryunosuke tazawa le 2 Août 2021
I'm sorry many times. Is the method of defining the function of ball_gosa correct? How can I call it?
ryunosuke tazawa
ryunosuke tazawa le 2 Août 2021
Although theta is recognized. Why are other functions or variables not recognized?
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 le 4 Août 2021
Thank you very much for your help. Thank you for kindly answering the questions to me who just started matlab.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by