Varying Variables within a Function
18 views (last 30 days)
Show older comments
The code that I currently have works perfectly for the variables that I have already assigned as constants.
%Setting the time interval in seconds
tspan = [0 20];
%Using the ODE Function
[t,x] = ode45(@Cart,tspan,[5 0]);
%Plotting the graph with a legend
plot(t,x(:,1),'DisplayName','Position'); hold on;
plot(t,x(:,2),'DisplayName','Velocity');
xlabel('Time'); ylabel('X-Values');
legend
%Writing out the function
function output = Cart(t,x)
m=2; %mass
b = 1.3; %damping
k = 20; %spring constant
x1prime = x(2);
x2prime = (-k*x(1)-b*x(2))/m;
output = [x1prime; x2prime];
end
Thank you for your time and help!
3 Comments
Walter Roberson
on 14 Nov 2020
Sam Zavala:
If you feel that your question is unclear, then since you are the author of the question, instead of flagging the question and hoping that a moderator will kindly take the time to figure out what you really meant and re-write the question to be better explained, then you should be spending the time to explain the question better yourself. You are the person who knows best what you need; you should not expect that someone else will spend the time making your question more clear.
Accepted Answer
Stephan
on 9 Nov 2020
Edited: Stephan
on 9 Nov 2020
%Setting the time interval in seconds
tspan = [0 20];
%% Define the values to iterate through
m = [2, 3, 4, 5, 6]; %mass --> vector with varied masses
b = [1.3, 1.5, 2, 2.5, 3]; %damping --> vector with varied damp
k = [20, 30, 40, 50, 60]; %spring constant
%% original values without vary
%Using the ODE Function
[t,x] = ode45(@(t,x)Cart(t,x,m(1),b(1),k(1)),tspan,[5 0]);
%Plotting the graph with a legend
subplot(2,2,1)
plot(t,x(:,1),'b','DisplayName','Position');
hold on;
plot(t,x(:,2),'r','DisplayName','Velocity');
xlabel('Time');
ylabel('X-Values');
legend('boxoff')
%% vary m
% Preallocate cell array for results of 5 runs with varying m
X_m = cell(numel(m),1);
% loop through the masses
% Note that the non varied values are the first entry in the vectors for
% m, b and k
for ii = 1:numel(m)
[t,x] = ode45(@(t,x)Cart(t,x,m(ii),b(1),k(1)),tspan,[5 0]);
X_m{ii} = [t, x];
end
% Plot the different values
subplot(2,2,2)
hold on;
for ii = 1:numel(m)
plot(X_m{ii,1}(:,1),X_m{ii,1}(:,2),'b');
plot(X_m{ii,1}(:,1),X_m{ii,1}(:,3),'r');
end
hold off
xlabel('Time'); ylabel('X-Values (m varied)');
legend({'Position','Velocity'})
legend('boxoff')
%% vary b
% Preallocate cell array for results of 5 runs with varying b
X_b = cell(numel(b),1);
% loop through the masses
for ii = 1:numel(b)
[t,x] = ode45(@(t,x)Cart(t,x,m(1),b(ii),k(1)),tspan,[5 0]);
X_b{ii} = [t x];
end
% Plot the different values
subplot(2,2,3)
hold on;
for ii = 1:numel(b)
plot(X_b{ii,1}(:,1),X_b{ii,1}(:,2),'b');
plot(X_b{ii,1}(:,1),X_b{ii,1}(:,3),'r');
end
hold off
xlabel('Time'); ylabel('X-Values (b varied)');
legend({'Position','Velocity'})
legend('boxoff')
%% vary k
% Preallocate cell array for results of 5 runs with varying k
X_k = cell(numel(k),1);
% loop through the masses
for ii = 1:numel(k)
[t,x] = ode45(@(t,x)Cart(t,x,m(1),b(1),k(ii)),tspan,[5 0]);
X_k{ii} = [t x];
end
% Plot the different values
subplot(2,2,4)
hold on;
for ii = 1:numel(k)
plot(X_k{ii,1}(:,1),X_k{ii,1}(:,2),'b');
plot(X_k{ii,1}(:,1),X_k{ii,1}(:,3),'r');
end
hold off
xlabel('Time'); ylabel('X-Values (k varied)');
legend({'Position','Velocity'})
legend('boxoff')
%% Writing out the function
function output = Cart(~,x,m,b,k)
x1prime = x(2);
x2prime = (-k.*x(1)-b.*x(2))./m;
output = [x1prime; x2prime];
end
2 Comments
Stephan
on 9 Nov 2020
The usual way is to try to avoid for loops and work with vectorization. This is nearly always the most efficient way.
But this
1) does not work always and
2) if it would work you still need to understand the code one year later.
So it may be possible, but there is also a trade off between understanding what the code does and being efficient.
More Answers (0)
See Also
Categories
Find more on Legend 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!