How do i add a function that needs inputs to the main code? (Nonlinear pendulum code)

1 visualización (últimos 30 días)
Hi guys,
I have the following two functions, pend_l and pend_n and the script file pend_solve.
the functions contain a variable "wsq" which needs to accept input values of L,M, b, so that the resulting value of wsq is substituted in the equations in
pend_l and pend_n.
I have been able to find the value of wsq as a seperate function, but i need to consolidate it with the rest of the code.
I do not know if i should add the expansion of wsq to the functions or to the script file, so that the output, with the plots and all, takes the value of wsq into consideration, after i input the values of L, M, and b.
Below is how i find the value of wsq seperately
function wsq = findwsq
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
wsq = (12*9.81*L)/((4*(L.^2))+(b.^2)-(12*L*M)+(12*(M.^2)));
disp(wsq)
end
My function files are:
pend_l
function xdot = pend_l(t1,x)
wsq=3.5; %i need to replace the constant value (arbitrarily taken as 3.5) of wsq with the above function
xdot = [x(2); -wsq*x(1)];
i need to replace the above wsq value with the function wsq such that pend_l computes xdot using the inputted values of L, M, b
similarly, i have
pend_n
function ydot = pend_n(t2,y)
wsq=3.5; % same value of wsq in both cases is required
ydot = [y(2); -wsq*sin(y(1))];
and my script file,
pend_solve
clear all;
clc;
clf;
tic;
tspan = 0:0.01:10;
a=pi/2;
b=0;
x0 = [a; b];
[t1,x] = ode45(@pend_l,tspan,x0);
X1 = x(:,1);
X2 = x(:,2);
y0 = [a ; b];
[t2,y] = ode45(@pend_n,tspan,y0);
Y1 = y(:,1);
Y2 = y(:,2);
figure(1);
subplot(2,2,1);
plot(t1,X1);
xlabel('Time (s)');
ylabel('Displacement (rad)');
hold on;
grid on;
plot(t2,Y1);
% legend('Linear','Non Linear');
subplot(2,2,2);
% figure(2);
plot(t1,X2);
xlabel('Time (s)');
ylabel('Velocity (rad/s)');
hold on;
grid on;
plot(t2,Y2);
subplot(2,2,3);
plot(X1,X2);
hold on;
plot(Y1,Y2);
xlabel('Displacement (rad)');
ylabel('Velocity (rad/s)');
grid on;
toc;
upon executing pend_solve with fixed value of wsq (without using the function wsq), i get three plots.
How do i make it so that the function wsq becomes a part of the main code such that the plots are plotted upon taking the computed value of wsq into consideration?

Respuesta aceptada

Moses Huang
Moses Huang el 11 de Nov. de 2020
To integrate wsq into your main code, you can do the following:
  1. Update the definition of pend_l and pend_n to take the wsq parameter:
function xdot = pend_l(t1,x,wsq)
xdot = [x(2); -wsq*x(1)];
end
function ydot = pend_n(t2,y,wsq)
ydot = [y(2); -wsq*sin(y(1))];
end
2. Update pend_solve to pass in wsq. Since ode45 only take function handles that accept two inputs, we have to define an anonymous function that calls pend_l and pend_n and pass in wsq to them. Relevant MATLAB documentation can be found here. Below I'm reproducing only a portion of your code and modified it:
wsq = 3.5; %Replace this with a call to findwsq function
[t1,x] = ode45(@(t,y) pend_l(t,y,wsq),tspan,x0);
...
[t2,y] = ode45(@(t,y) pend_n(t,y,wsq),tspan,y0);
...
3. Now you can use findwsq function to set wsq according to user input
  1 comentario
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS el 12 de Nov. de 2020
I'm sorry, but point 2 is not very clear to me. Could you please elaborate more simply?
Also, why
[t1,x] = ode45(@(t,y) pend_l(t,y,wsq),tspan,x0);
does this function have (t,y) in the bracket instead of (t,x)?

Iniciar sesión para comentar.

Más respuestas (1)

KLETECH MOTORSPORTS
KLETECH MOTORSPORTS el 12 de Nov. de 2020
i tried modifying it the way you explained but i think i have made a mistake somewhere:
pend_l
function xdot = pend_l(t1,x,wsq)
xdot = [x(2); -wsq*x(1)];
end
pend_n
function ydot = pend_n(t2,y,wsq)
ydot = [y(2); -wsq*sin(y(1))];
end
findwsq
function wsq = findwsq(L,b,M)
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
wsq = (12*9.81*L)/((4*(L.^2))+(b.^2)-(12*L*M)+(12*(M.^2)));
disp(wsq)
end
pend_solve
clc;
clf;
tic;
tspan = 0:0.01:10;
a=pi/2;
b=0;
x0 = [a; b];
findwsq(wsq) %callinf findwsq here
[t1,x] = ode45(@(t1,x) pend_l(t1,x,wsq),tspan,x0);
X1 = x(:,1);
X2 = x(:,2);
y0 = [a ; b];
findwsq(wsq) %caling findwsq here
[t2,y] = ode45(@(t2,y) pend_n(t2,y,wsq),tspan,y0);
Y1 = y(:,1);
Y2 = y(:,2);
figure(1);
subplot(2,2,1);
plot(t1,X1);
xlabel('Time (s)');
ylabel('Displacement (rad)');
hold on;
grid on;
plot(t2,Y1);
legend('Linear','Non Linear');
subplot(2,2,2);
% figure(2);
plot(t1,X2);
xlabel('Time (s)');
ylabel('Velocity (rad/s)');
hold on;
grid on;
plot(t2,Y2);
subplot(2,2,3);
plot(X1,X2);
hold on;
plot(Y1,Y2);
xlabel('Displacement (rad)');
ylabel('Velocity (rad/s)');
grid on;
toc;
didn't know how to call an anonymous function, but i tried writing this:
i'm positive this is wrong but i didn't know what else to do
function anon = manyinput(pend_l,pend_n,findwsq)
end
  2 comentarios
Moses Huang
Moses Huang el 13 de Nov. de 2020
I see two issues. 1) Because you are prompting the user to enter L, M and b, you don't need to include them as inputs to findwsq:
function wsq = findwsq()
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
wsq = (12*9.81*L)/((4*(L.^2))+(b.^2)-(12*L*M)+(12*(M.^2)));
disp(wsq)
end
2) In pend_solve, you need to call findwsq() and assign it to wsq, which will be passed to pend_l and pend_n
clc;
clf;
tic;
tspan = 0:0.01:10;
a=pi/2;
b=0;
x0 = [a; b];
wsq = findwsq();
[t1,x] = ode45(@(t1,x) pend_l(t1,x,wsq),tspan,x0);
X1 = x(:,1);
X2 = x(:,2);
y0 = [a ; b];
[t2,y] = ode45(@(t2,y) pend_n(t2,y,wsq),tspan,y0);
Y1 = y(:,1);
Y2 = y(:,2);
figure(1);
subplot(2,2,1);
plot(t1,X1);
xlabel('Time (s)');
ylabel('Displacement (rad)');
hold on;
grid on;
plot(t2,Y1);
legend('Linear','Non Linear');
subplot(2,2,2);
% figure(2);
plot(t1,X2);
xlabel('Time (s)');
ylabel('Velocity (rad/s)');
hold on;
grid on;
plot(t2,Y2);
subplot(2,2,3);
plot(X1,X2);
hold on;
plot(Y1,Y2);
xlabel('Displacement (rad)');
ylabel('Velocity (rad/s)');
grid on;
toc;
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS el 15 de Nov. de 2020
Thank you Moses, i have made the necessary changes and it's working fine.

Iniciar sesión para comentar.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by