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

1 view (last 30 days)
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 Comments
DGM
DGM on 1 Oct 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 on 1 Oct 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.

Sign in to comment.

Answers (1)

Jan
Jan on 1 Oct 2021
Edited: Jan on 1 Oct 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.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!