error "Error in untitled (line 9)"

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

Respuestas (1)

Stephen23
Stephen23 el 11 de Jul. de 2023
Editada: Stephen23 el 11 de Jul. de 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 comentarios

felipe
felipe el 11 de Jul. de 2023
Editada: felipe el 11 de Jul. de 2023
Thank you for your help! It works.
Although, I found some simpler solution.
Define [t,x] = ode45(@(t, x) prob(t, x), tspan, yo);
Will fix it.
Stephen23
Stephen23 el 11 de Jul. de 2023
"Although, I found some simpler solution. "
It is unclear to me how you think that creating an anonymous function like this:
[t,x] = ode45(@(t, x) prob(t, x), tspan, yo);
is a "simpler solution" than the (actually simpler) function handle that I showed in my answer:
[t,x] = ode45(@prob, tspan, x0);
It seems that perhaps we use different meanings of the word "simpler".
@prob will execute more quickly than @(t,x) prob(t,x)
f1 = @prob
f1 = function_handle with value:
@prob
f2 = @(t,x) prob(t,x)
f2 = function_handle with value:
@(t,x)prob(t,x)
functions(f1)
ans = struct with fields:
function: 'prob' type: 'simple' file: ''
functions(f2)
ans = struct with fields:
function: '@(t,x)prob(t,x)' type: 'anonymous' file: '/tmp/Editor_utsta/LiveEditorEvaluationHelperEeditorId.m' workspace: {[1×1 struct]} within_file_path: ''
Mathworks literally calls the first version simple .
Each time an anonymous function handle is called, MATLAB has to go through the work of injecting the values in workspace into the workspace of the called function. That is extra overhead that is not required for simple function handles.
felipe
felipe el 11 de Jul. de 2023
Actually, my solution and yours have the same result. I was trying to solve another equation where dx(2) involved some term explicitly in "t" and the @(t,x) prob(t,x) works fine.
felipe
felipe el 11 de Jul. de 2023
Using the followinf code I got the figure bellow.
%Numerical Solutions
%Problem #57
clc
clear
close all
%Numerical Solution
x0=[0;0];
tspan=[0 15];
[t,x]=ode45(@(t,x) prob(t,x),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,5);
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;
%dx = [x(2);-c/m*x(2)-k/m*x(1)+F/m];
%end
end
end
felipe
felipe el 11 de Jul. de 2023
It's absolutely clear that I chose the wrong word to use. I was no intend to underestimate no one.
felipe
felipe el 11 de Jul. de 2023
I got the exactly some answer using the following code, with your suggestions.
Thank you again!
%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,5);
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;
dx = [x(2);-c/m*x(2)-k/m*x(1)+F/m];
end
end
Walter Roberson
Walter Roberson el 11 de Jul. de 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 el 12 de Jul. de 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:

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Jul. de 2023

Comentada:

el 12 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by