Matlab function HW help please

In summary, the conversation discusses the creation of a function to compute distance x using input parameters W, k1, k2, and d. The formulas for x are given as x=W/k1 and x=(W+2k2d)/(k2+2k2), but the speaker is having trouble solving for x due to an error saying that x is not defined. The conversation also touches on the concept of conditions on dependent variables and how to properly evaluate functions.
  • #1
atrid32
1
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
  • #2
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
 
  • #3


Hi there,

Thank you for reaching out for help with your Matlab function. I understand that you are trying to create a function that computes the distance x using the input parameters W, k1, k2, and d. Let's take a look at your code and see where the issue might be.

First, let's look at your if/else statement. In your code, you are trying to use the variable x in the if statement without defining it first. This is why you are getting an error saying that x is not defined. To fix this, you need to define x before using it in the if statement. Also, in your else statement, you have used a condition (x >= 0.1) instead of an assignment (x = 0.1). This might be causing the error as well.

Second, in your code, you have used the variable W in the calculation of x, but you have not included it as an input parameter for your function. Therefore, when you call your function, you will get an error saying that there are not enough input arguments. To fix this, you need to include W as an input parameter in your function declaration (after the function name and inside the parentheses).

Third, in your code, you have used the variable d in the calculation of x, but you have not included it as an input parameter for your function. This will also cause an error when you call your function. To fix this, you need to include d as an input parameter in your function declaration (after W and separated by a comma).

Finally, in your else statement, you have used the variable k1 instead of k2 in the denominator of the second equation. This is a typing error and can be easily fixed.

Here is the corrected code:

function x = spring (W, d)
k1=10^4;
k2=1.5*10^4;
if x < d
x=W/k1;
else
x=(W+(2*k2)*d)/(k2+(2*k2));
end

Now, when you call your function and provide the necessary input arguments (W and d), it should work properly. For example, if you want to compute the distance x for W=500, you can use the following code:

x = spring(500, 0.1)

I hope this helps! Please let me know if you have any further questions or if you need any additional
 

1. What is a Matlab function?

A Matlab function is a group of commands written in the Matlab programming language that can be called and executed to perform a specific task or calculation.

2. How do I create a Matlab function?

To create a Matlab function, you need to first create a new file and save it with a .m extension. Then, you can write your function code using the correct syntax and save the file. The function can then be called and used in other scripts or the command window.

3. What is the syntax for a Matlab function?

The basic syntax for a Matlab function is as follows:
function [outputArg1, outputArg2, ...] = functionName(inputArg1, inputArg2, ...)
% Function body
end
The function name, input and output arguments, and function body must be properly defined and indented for the function to work correctly.

4. How do I use input and output arguments in a Matlab function?

Input and output arguments are used to pass data into and out of a Matlab function. Input arguments are specified within the function declaration and can be used within the function body. Output arguments are specified within square brackets after the function name and can be used to return values from the function.

5. Can a Matlab function have multiple output arguments?

Yes, a Matlab function can have multiple output arguments. These can be specified within square brackets after the function name and separated by commas. The function must then return the same number of output arguments in the same order as they were specified.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
5
Views
310
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Astronomy and Astrophysics
Replies
2
Views
965
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
0
Views
584
Back
Top