error "Error in untitled (line 9)"
조회 수: 5 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Stephen23
2023년 7월 11일
편집: Stephen23
2023년 7월 11일
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
Walter Roberson
2023년 7월 11일
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
2023년 7월 12일
"For historical backwards compatibility..."
That must be quite historical, for more than ten years the 1st input is only documented as a function handle:
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

