Varying Variables within a Function

조회 수: 2 (최근 30일)
Sam Zavala
Sam Zavala 2020년 11월 9일
댓글: Sam Zavala 2020년 11월 14일
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
Walter Roberson
Walter Roberson 2020년 11월 14일
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.
Sam Zavala
Sam Zavala 2020년 11월 14일
Agreed, that was on me, I ended up flagging it on accident.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephan
Stephan 2020년 11월 9일
편집: Stephan 2020년 11월 9일
%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
Stephan
Stephan 2020년 11월 9일
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.
Sam Zavala
Sam Zavala 2020년 11월 10일
That makes a lot of sense. I try to include comments in my code and break it up as much as I can so it seems more like a step by step walk through rather than the headline for an entire paragraph because I try to keep in mind that it would be good to look back on it much later. I'll try to keep that in mind!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by