How can I show a graph from the function?

Nc=100;
c = linspace(10,60,Nc)
function [t,th,pass] = lab1(c0,k0)
J=20;
opt=[];
F0=0;
omega=0;
dt=0.01; % sampling time interval
npt=2000; % number of sampling points
tspan=(1:npt)*dt; % time instants where x(t) is simulated
idx=0;
pass=[];
th_lim1=65/180*pi; % [rad]
th_lim2=3/180*pi; % [rad]
b_max=3.5; % [rad/s]
k=k0;
for i=1:length(c0)
for j=1:length(k0)
ctmp=0;
ktmp=0;
c=c0(i);
k=k0(j);
th0=[0;b_max];
[t,th]=ode45(@sdof,tspan,th0,opt,J,c,k,F0,omega);
end
end
end
function G=sdof(t,x,J,c,k,F0, omega)
the code above are function .
G=[x(2);(-c*x(2)-k*x(1)+F0*cos(omega*t)//J];
end
Invalid use of operator.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
This code below is the plotting for the function .
plot(t,th(:,1)*180/pi)
title('Displacement vs Time of a Swinging Door')
xlabel('Time(Seconds)')
ylabel('Displacement(Degrees)')
plot([0,20],[1,2]*(65));
Attempt to execute SCRIPT lab1 as a function:
C:\Users\joe_p\Desktop\mec721\lab1.m
Error in lab1 (line 1)
[t,th,pass]=lab1(10,30)
That is error .
Please someone explain why i keep getting this error and explain how i can get the graph to work.

回答(2 个)

G=[x(2);(-c*x(2)-k*x(1)+F0*cos(omega*t)/J]; % seems you have used // erroneously
end
it is a division operator unlike a comment in C++ language

4 个评论

Call the function from command window instead of running from script
>>lab1(10:60,30:60)
Attempt to execute SCRIPT lab1 as a function:
C:\Users\joe_p\Desktop\mec721\lab1.m
Nc=10;
c0 = linspace(10,60,Nc);
k0 = linspace(0,20,Nc/2);
[t,th,pass] = lab1(c0,k0)
t = 2000×1
0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000
th = 2000×2
0 1.0000 0.0075 0.5481 0.0116 0.2993 0.0139 0.1624 0.0151 0.0869 0.0157 0.0454 0.0160 0.0225 0.0162 0.0099 0.0163 0.0030 0.0163 -0.0008
pass = []
plot(t,th(:,1)*180/pi)
title('Displacement vs Time of a Swinging Door')
xlabel('Time(Seconds)')
ylabel('Displacement(Degrees)')
function [t,th,pass] = lab1(c0,k0)
J=20;
opt=[10; 20];
F0=0;
omega=0;
dt=0.01; % sampling time interval
npt=2000; % number of sampling points
tspan=(1:npt)*dt; % time instants where x(t) is simulated
idx=0;
pass=[];
th_lim1=65/180*pi; % [rad]
th_lim2=3/180*pi; % [rad]
b_max=3.5; % [rad/s]
k=k0;
for i=1:length(c0)
for j=1:length(k0)
ctmp=0;
ktmp=0;
c=c0(i);
k=k0(j);
th0=[0;b_max];
[t,th]=ode45(@(th0,opt) sdof(th0,opt,J,c,k,F0,omega),tspan,[0 1]);
end
end
function G=sdof(t,x,J,c,k,F0, omega)
G=[x(2);-c*x(2)-k*x(1)+F0*cos(omega*t)/J];
end
end

请先登录,再进行评论。

Attach your whole m-file.
You can't have a script followed by functions unless the functions end with "end", which yours don't. Your for loops end with end but the functions don't. Here is your script:
Nc=100;
c = linspace(10,60,Nc)
Notice that the script never even calls any of the functions defined below it in the file.
The m-file should look like
Nc=100;
c0 = linspace(10,60,Nc)
k0 = 1; % Whatever...
% Now call the main function:
[t,th,pass] = lab1(c0,k0)
% Now define all your functions AFTER your script
% and finish each one with an "end" statement.
%=====================================================================================
function [t,th,pass] = lab1(c0,k0)
% Its code...
end % Need final closing end statement
%=====================================================================================
function G=sdof(t,x,J,c,k,F0, omega)
% Its code...
end % Need final closing end statement

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by