error "Error in untitled (line 9)"

5 visualizzazioni (ultimi 30 giorni)
felipe
felipe il 11 Lug 2023
Commentato: Stephen23 il 12 Lug 2023
I'm trying to run the following code, byt I have the error "Error in untitled (line 9) [t,x]=ode45('prob',tspan,x0);" .
Why is that happening? Can anyone help me, please? Thank you!
%Numerical Solutions
%Problem #57
clc
clear
close all
%Numerical Solution
x0=[0;0];
tspan=[0 15];
[t,x]=ode45('prob',tspan,x0);
figure(1)
plot(t,x(:,1));
title('Problem #57');
xlabel('Time, sec.');
ylabel('Displacement, m');
hold on
%Analytical Solution
m=100;
c=20;
k=1000;
F=30;
w=sqrt(k/m);
d=c/(2*w*m);
wd=w*sqrt(1-d^2);
to=1;
phi=atan(d/sqrt(1-d^2));
%for t<to
t=linspace(0,1,3);
x=0.*t;
plot(t,x,'*');
%for t>=to
t=linspace(1,15);
x=F/k-F/(k*sqrt(1-d^2)).*exp(-d.*w.*(t-to)).*cos(wd.*(t-to)-phi);
plot(t,x,'*');
legend('Numerical', 'Analytical')
%M-file for Prob #50
function dx=prob(t,x)
[rows, cols]=size(x);dx=zeros(rows, cols);
m=100;
c=20;
k=1000;
F=30;
if t<1
dx=0;
else
dx(1)=x(2);
dx(2)=-c/m*x(2) - k/m*x(1) + F/m;
end
end

Risposte (1)

Stephen23
Stephen23 il 11 Lug 2023
Modificato: Stephen23 il 11 Lug 2023
The ODE45 documentation states that its first input argument must be a function handle:
Therefore you should provide its first input argument as a function handle. That will avoid the error that you show... but then you will get other errors, due to the sizes of the output returned by that function... I fiddled around to avoid those (i.e. you need to check):
%Numerical Solutions
%Problem #57
%Numerical Solution
x0=[0;0];
tspan=[0 15];
[t,x]=ode45(@prob,tspan,x0);
figure(1)
plot(t,x(:,1));
title('Problem #57');
xlabel('Time, sec.');
ylabel('Displacement, m');
hold on
%Analytical Solution
m=100;
c=20;
k=1000;
F=30;
w=sqrt(k/m);
d=c/(2*w*m);
wd=w*sqrt(1-d^2);
to=1;
phi=atan(d/sqrt(1-d^2));
%for t<to
t=linspace(0,1,3);
x=0.*t;
plot(t,x,'*');
%for t>=to
t=linspace(1,15);
x=F/k-F/(k*sqrt(1-d^2)).*exp(-d.*w.*(t-to)).*cos(wd.*(t-to)-phi);
plot(t,x,'*');
legend('Numerical', 'Analytical')
%M-file for Prob #50
function dx=prob(t,x)
m=100;
c=20;
k=1000;
F=30;
dx = [x(2);-c/m*x(2)-k/m*x(1)+F/m];
end
  9 Commenti
Walter Roberson
Walter Roberson il 11 Lug 2023
The ODE45 documentation states that its first input argument must be a function handle:
For historical backwards compatibility, ode45 permits the first input argument to be a character vector that is the name of the function to use. However, if that is used, then the function named must be accessible from the command line, so effectively the name used must be the name of a .m file (or similar). In particular, when you use a character vector, ode45 cannot look inside your current file to locate a function with the name given.
Stephen23
Stephen23 il 12 Lug 2023
"For historical backwards compatibility..."
That must be quite historical, for more than ten years the 1st input is only documented as a function handle:

Accedi per commentare.

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by