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

1 view (last 30 days)
Nirmith Kumar Mishra
Nirmith Kumar Mishra on 1 Oct 2021
Edited: Jan on 1 Oct 2021
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
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.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by