I have a code that I give input to and it gives me an output. The problem is that I don't know what the input is but I know what the output should be. So I am trying to use a while loop to find the input variable by trying different values for the input variable until the output variable is reached. Specifically, I am trying to increase the input variable by increments of 0.1 starting from zero. I have mage a while loop but I keep getting the error "Function definition not supported in this context. Create functions in code file." for line 4 in my code. This is my code below:
B=0;
A=0;
while A<1.1345
function Lab
clear all; close all; clc;
j=20; % moment of inertia (kg.m^2)
k=30; % rotational stiffness (N.m/rad)
c=10; % damping constant (N.m.s)
xo=0; % initial angular postion
vo=B+0.1; % initial angular velocity
x0=[xo vo]; %Initial condition vector
% Function second_order_ODE
% Numerical simulation Using Runge-Kutta
t1=0; % Starting time (s)
t2=20; %Ending time (s)
dt=0.05; % Integration time step (s)
t=t1:dt:t2;
[t,x]=ode45(@rhs,t,x0,[],j,c,k);
% Plotting the output
figure;
subplot(211);
plot(t,x(:,1),'b-');
xlabel('Time (s)'); ylabel('Angular Displacement (rad)');
subplot(212);
plot(t,x(:,2),'b-');
xlabel('Time (s)'); ylabel('Angular Velocity (rad/s)')
function G=rhs(t,x,j,c,k)
G(1,1)=x(2);
G(2,1)=(-k*x(1)-c*x(2))/j;
end
A=max(x(:,1));
end
end

 채택된 답변

Star Strider
Star Strider 2020년 10월 11일
편집: Star Strider 2020년 10월 11일

1 개 추천

You can get around the function problem by creating ‘rhs’ as an anonymous function:
rhs = @(t,x,j,c,k) [x(2); (-k*x(1)-c*x(2))/j];
Then put it before the beginning of the while loop, and change the ode45 call to:
[t,x]=ode45(@(t,x)rhs(t,x,j,c,k),t,x0);
There are likely much more efficient ways to optimise your differential equation that the exhaustive-serach approach.
It would help to know what you are doing, and what you want to optimise for, and what parameters you are changing to do the optimisation. That is not clear from your code.
EDIT — (11 Oct 2020 at 14:00)
In the interim, I would change to this version of your code:
B=0;
A=0;
t1=0; % Starting time (s)
t2=20; %Ending time (s)
dt=0.05; % Integration time step (s)
t=t1:dt:t2;
rhs = @(t,x,j,c,k) [x(2); (-k*x(1)-c*x(2))/j];
k = 1
while A<1.1345
j=20; % moment of inertia (kg.m^2)
k=30; % rotational stiffness (N.m/rad)
c=10; % damping constant (N.m.s)
xo=0; % initial angular postion
vo=B+0.1; % initial angular velocity
x0=[xo vo]; %Initial condition vector
% Function second_order_ODE
% Numerical simulation Using Runge-Kutta
[t,x]=ode45(@(t,x)rhs(t,x,j,c,k),t,x0);
A=max(x(:,1));
Av(k) = A;
k = k + 1;
end
% Plotting the output
figure
subplot(211);
plot(t,x(:,1),'b-');
xlabel('Time (s)'); ylabel('Angular Displacement (rad)');
subplot(212);
plot(t,x(:,2),'b-');
xlabel('Time (s)'); ylabel('Angular Velocity (rad/s)')
.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Guidance, Navigation, and Control (GNC)에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2020년 10월 11일

편집:

2020년 10월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by