Solving for a value using a while loop.
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Guidance, Navigation, and Control (GNC) 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!