How to pass a variable input into a function in matlab?

In summary, this function simulates a bouncing ball by modifying the event that occurs after each bounce.
  • #1
supernova1387
31
0
I have an ode function which is of the form:

function [output]=Fun[input1,input2,input3]

which is solving a set of non-linear differential equations. input1 and 3 are fixed but input2 is a variable. Say:

input1=3; % a constant
input3=20;
input2=[2 4 6 8];

function Fun is being called in an m.file called main. The ode is being solved 20 times (input3 defines number of repetition). I want Fun to use a different element of vector input2 each time and repeat this process again and again until the number of input3. That is:

first time solving : input2=2;
second time solving: input2=4;
.
.
.
fifth time solving: input5=2 again

and this process must repeat until Fun reaches input3=20

Any suggestions are welcome.
 
Physics news on Phys.org
  • #2
Hey supernova1387.

This seems like a simple function with a loop and a function call inside of it.

Can you show us any code you have currently? Do you know how to use loops and how to use the counters in the loops for passing to other functions as arguments?
 
  • #3
chiro said:
Hey supernova1387.

This seems like a simple function with a loop and a function call inside of it.

Can you show us any code you have currently? Do you know how to use loops and how to use the counters in the loops for passing to other functions as arguments?

Thank you for the reply

The code is quite long and complicated. I can give you a simpler example. Imagine we want to simulate a bouncing ball where input3 defines number of bounces. We have equations of motion, impact phase and event. The event occurs when y=0(the ball contacts the ground). Now what I want to do is to change this event for each bounce. Let's say I have 20 bounces but my ground is moving up and down too so the event occurs at y=0.5, 0.1 ,0 etc rather than always at y=0. In other words, y is a vector rather than a single number. Let's say:
y=[0.5, 0.1, 0, -0.1, 0.1, -0.5]

It is kind of like a rough surface where y defines the height of the roughness and the ball contacts the floor when the height of the ball is equal to the elements of y-matrix at each bounce. The y-matrix has just 6 elements but it will repeat itself. That is after the 6th bounce, y would be y=0.5 again.

I just consider motion is y-direction and assume the ball will just bounce up and down. Any suggestions?
 
  • #4
Well you can construct a simple loop within a loop to do this. Basically if you have 20 bounces but a vector with 5 entries, the outer most loop does 20/5 = 4 iterations and the inner loop does 5 iterations (or the size of your vector).

If you want to re-use inputs or do periodic calculations like the one above: you can either use another loop or use the modulus function. Using the modulus function for i = 0 to 19 mod 5 would give 0,1,2,3,4,0,1,2,3,4 and so on and you can use this as an index.
 
  • #5


One way to pass a variable input into a function in MATLAB is to use a loop. In this case, you can create a for loop in your main file that will call the function Fun with a different value of input2 each time. For example:

for i=1:input3
Fun(input1, input2(i), input3);
end

This will call Fun 20 times, each time with a different value of input2 from the vector. Another option is to use a function handle, which allows you to pass a function as a variable. You can create a function handle for Fun and then use it in a loop to call the function with different inputs. For example:

myFun = @(x) Fun(input1, x, input3);

for i=1:input3
myFun(input2(i));
end

Both of these methods will allow you to pass a variable input into the function and repeat the process until input3 is reached.
 

1. How do I declare a variable input in a function in Matlab?

To declare a variable input in a function in Matlab, you can use the "varargin" keyword in the function declaration. This will allow you to pass in an unspecified number of input arguments into the function.

2. How do I pass a specific variable as an input into a function in Matlab?

To pass a specific variable as an input into a function in Matlab, you can specify the variable name and value in the function call. For example, if the function is called "myFunction" and the variable is named "x", the function call would look like this: myFunction('x', 5).

3. Can I pass multiple variables into a function in Matlab?

Yes, you can pass multiple variables into a function in Matlab by using the "varargin" keyword and specifying the variable names and values in the function call.

4. How do I use the input variables within the function in Matlab?

To use the input variables within the function in Matlab, you can access them using the "varargin" keyword and the corresponding variable name. For example, if the function is called "myFunction" and the variable is named "x", you can use the input variable by accessing it as "varargin{2}" within the function.

5. Is there a limit to the number of input variables that can be passed into a function in Matlab?

No, there is no limit to the number of input variables that can be passed into a function in Matlab. You can use the "varargin" keyword to pass in any number of input arguments into the function. However, it is important to note that too many input variables can make the code more complex and difficult to debug.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
940
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
687
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top