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

Click For Summary

Discussion Overview

The discussion revolves around how to pass variable inputs into a function in MATLAB, specifically in the context of solving differential equations and simulating a bouncing ball with varying conditions. Participants explore methods for iterating through inputs and managing function calls within loops.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes a function that solves non-linear differential equations with fixed and variable inputs, seeking advice on how to iterate through a vector of variable inputs.
  • Another participant suggests using a loop to manage the function calls and passing the variable input, asking for existing code to better understand the user's approach.
  • A further example is provided by the original poster, illustrating a bouncing ball simulation where the event condition changes with each bounce, using a vector for varying heights.
  • A participant proposes a nested loop structure to handle the iterations of bounces and suggests using the modulus function to cycle through the vector of heights effectively.

Areas of Agreement / Disagreement

Participants generally agree on the use of loops to manage variable inputs, but there is no consensus on the specific implementation details or the best approach to take.

Contextual Notes

Participants have not provided complete code examples, and assumptions about the structure of the functions and the nature of the inputs remain unspecified. The discussion includes varying levels of complexity in the examples presented.

Who May Find This Useful

This discussion may be useful for MATLAB users interested in function input management, particularly in the context of simulations involving iterative processes and variable conditions.

supernova1387
Messages
30
Reaction score
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
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?
 
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?
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K