How can I show a graph from the function?
Show older comments
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
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.
Answers (2)
VBBV
on 31 Jan 2022
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 Comments
Call the function from command window instead of running from script
>>lab1(10:60,30:60)
Perry fung
on 31 Jan 2022
Perry fung
on 31 Jan 2022
Nc=10;
c0 = linspace(10,60,Nc);
k0 = linspace(0,20,Nc/2);
[t,th,pass] = lab1(c0,k0)
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
Image Analyst
on 31 Jan 2022
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
Categories
Find more on Designs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!