How to Solve for x in a MATLAB Function Using Given Parameters?

  • Context: MATLAB 
  • Thread starter Thread starter atrid32
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary
SUMMARY

The discussion centers on creating a MATLAB function to compute the variable x based on given parameters W, k1, k2, and d. The user initially attempts to define x within an if/else structure but encounters an error due to the undefined state of x at the time of comparison. The correct approach involves evaluating x based on the independent variable W, leading to the formulas x=W/k1 for x=d. The solution emphasizes the importance of defining independent variables before making conditional evaluations.

PREREQUISITES
  • Understanding of MATLAB function syntax
  • Knowledge of conditional statements in programming
  • Familiarity with mathematical modeling concepts
  • Basic grasp of variable scope and initialization in programming
NEXT STEPS
  • Review MATLAB function creation and variable scope
  • Learn about conditional statements and their proper usage in MATLAB
  • Explore mathematical modeling techniques for defining dependent and independent variables
  • Investigate error handling in MATLAB to troubleshoot similar issues
USEFUL FOR

Mathematics students, MATLAB programmers, and engineers involved in computational modeling who need to understand function evaluations and variable dependencies.

atrid32
Messages
1
Reaction score
0
Hi, i need to write function for the following.
W=k1x if x<d
W=k1x+2k2(x-d) if x>=d

Question is: Create function that computes distance x using the input parameters W, k1, k2, and d.
k1=10^4
k2=1.5*10^4
d=0.1
W=500 and 2000
so i get the formulas solving for x that are
x=W/k1 and x=(W+2k2d)/(k2+2k2)
but I don't know how to solve for x as I get error which says that x is not defined when i use if/else and i tried using for loop and got the same problem.
here is my code:
function x = spring (W)
k1=10^4;
k2=1.5*10^4;
d=0.1;
if x < 0.1
x=W/k1
else x >= 0.1
x=(W+(2*k2)*d)/(k1+(2*k2))
end

Any help please?
 
Physics news on Phys.org
Hi,
I see the problem not with the code but with the concept of this calculation it self. If you took a look on what you have written it's obvious why doesn't it work.
How can you evaluate a function depending on the output of the function?
you say for example
y = 3 * x if y < 5
y = 5 * x + 7 if y >= 5
this conditions can't be on the dependent variable it can only be on the independent variable you can calculate it easily.
so y = 3 * x if x < 5/3
and y = 5 * x + 7 if x >= -2/5

I know that's are not mutual exclusive conditions. Which in fact what you are doing in first place?

but why MATLAB say this error?
review the code:
Code:
function x = spring (W)
k1=10^4;
k2=1.5*10^4;
d=0.1;
if x < 0.1
see until this comparison you haven't made any evaluation for the x.
If your calculation are recurrsive - depend on the previous value of x -, then you can assume an inital value for x to make this comparison

That's all,
bye
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K