A Matlab Code is "Attempt to execute SCRIPT ballTrajectoryFun as a function:"

1 visualización (últimos 30 días)
clc
clear all
close all
%% Define Parameters and Initial Conditions
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);
%% Setting up and Solving the problem
X0 = [0; 0; % starting position is the origin
u0; v0]; % starting velocity is given
tSpan = [0 20]; % simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);
%% Displaying the results
figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');
%% Animating results
exitCode = ballAnimation(tOut,XOut);
  2 comentarios
DGM
DGM el 1 de Oct. de 2021
The error means what it says. There is a script on the path called ballTrajectoryFun
which ballTrajectoryFun -all
Either this script is shadowing a function of the same name, or it's an improperly defined function file. If the latter, you'll have to reveal how the file starts (i.e. what the function definition line is). If there is no function definition, then it needs one.
Rik
Rik el 1 de Oct. de 2021
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
Also, the first three lines of your code indicate that you probably didn't think through what you want to happen. Why are you clearing the command window? You aren't printing anything to it. Why are you clearing not just variables, but everything? Are you restarting Matlab every time you run this script? And why are you closing all figures? You could simply use clf to clear figure 1 after opening it.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 1 de Oct. de 2021
Editada: Jan el 1 de Oct. de 2021
The file ballTrajectoryFun.m is a script, not a function. Functions start with the keyword "function". If they are called from ODE45, they must accept at least 2 inputs and reply at least one output.
Providing the parameters as 5th input is outdated for over 10 years. Use an anonymous function instead:
[tOut, XOut] = ode45(@(t, y) ballTrajectoryFun(t, y, param), tSpan, X0);
And the function to be integrated starts with:
function dy = ballTrajectoryFun(t, y, param)
...
end
Do not use clear all in productive code, because it wastes a lot of time. For short hacks a clearvars might be okay instead.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by