Solving a Function Error with x1,x2 Inputs

In summary: UTF-8Hi,I'm trying to put a equation in a function. So i can just type this function file name into another to find out the answers. This is what i got from the question:function y = f1(x)y=f1(x)x=(x1,x2)f1(x1,x2)=x1*5+x2*3what i did:function y = f1(x)x=[x1,x2];y=x1*5+x2*3;endIt said Error in ==> f
  • #1
hoheiho
47
0
Hi,
I am trying to put a eqn in a function. So i can just type this function file name into another to find out the answers. This is what i got from the question:
function y = f1(x) %required function name
y=f1(x)
x=(x1,x2)
f1(x1,x2)=x1*5+x2*3

what i did :
function y = f1(x)
x=[x1,x2];
y=x1*5+x2*3;
end

It said Error in ==> f1 at 2 x = [x1,x2];
what I am thinking is y is output and x is input here. So i declare the x to be x1,x2 and then declare the eqn of y. What should i do to fix this out?

Thanks
 
Physics news on Phys.org
  • #2
I'm not quite sure what you're doing, you should copy the exact error and tell more about with what line the error occured, but I see a problem right off. you have 'f1' defined as both a function and variable. If you want 'f1' to be your function name, then you shouldn't use the line:

f1(x1,x2)=x1*5+x2*3

In fact, any time you try a new simple word variable in MATLAB, it's always good to throw it to the command line first and see if it's a known function (or throw a 'help' or 'doc' command at it). Many functions built into MATLAB have short simple words that can be overwritten or accidentally called if you write your own function with the same name or try to use the name as a variable.
 
  • #3
Thanks for your reply. What i have to do is write 2 functions.
function [X, y] = optimise(function_name, x0, delta) and function y = f1(x)
in function y = f1(x), the x value eqn is given which is f1(x1,x2)=x1*5+x2*3. x=(x1,x2) is telling us there is two x value in the f1(x) function.

My MATLAB code is :

function y = f1(x)
x=[x1,x2];
y=x1*5+x2*3;
end

The error message i got :

? Undefined function or variable 'x1'.

Error in ==> f_1 at 2
x = [x1,x2];

I hope i make it clear now. Sorry for the poor english :(
 
  • #4
Well, you haven't defined the values of x1 and x2.
Remember that Matlab is NOT a symbolic language (unless you use that toolbox, but you are not). Hence, x=[x1, x2] only makes sense if x1 and x2 have already been defined and have some definite value. Moreover, Matlab always works with matrices that can be indexed directly, meaning if you have a vector 2x1 vector by the name of x, you can access its element by x(1) and x(2).

Hence, in your case you could write

Code:
function y = f1(x)
y=x(1)*5+x(2)*3;
end


f1([3 2]) would now return the value for f1 for x=[3,2].
 
  • #5
thanks for your time:) i think i got it now
just to make sure : If the value have not been defined so i need to use x(1),x(2). if not, i can use x=[x1,x2].
 
  • #6
No, not quite

Look at the function. It starts with the statement

y=f1(x)

This tells Matlab that this is a function that accepts a variable x and returns some value y. Hence, the variable x is defined. This means that when you write x(1) and x(2) Matlab knows which variable you are referring to.

In your original program you wrote x=[x1, x2]; but this tells Matlab "set the two components of the variable x to whatever the values of x1 and x2 are". However, x1 and x2 were not defined anywhere in your program and therefore had no values.
 
  • #7
Thank you for your clear explanation :). I understand much more now
 
  • #8
Dear sir,
i have an ECG project analog signal , i want to display this signal by using matlab
what are the procedurs can i do?
 
  • #9
load(file) if its an ascii file, should load easily, takes string arguments

plot(x)
where x is the variable of your signal

try help load and help plot if you have trouble
 
  • #10
i have an ECG project and want to disply on matlab
in the first did the hardware part and got the signal on the osciloscope as analog singal so this is i want, how to deal with this signal to display on MATLAB by using parralel port of the compurt
 

1. What is a function error?

A function error is an error or mistake that occurs when trying to solve a mathematical function. It can happen for a variety of reasons, such as incorrect input values or an error in the function itself.

2. How do I solve a function error with x1 and x2 inputs?

To solve a function error with x1 and x2 inputs, you first need to identify the specific error that is occurring. This could be a syntax error, a division by zero error, or an undefined value error. Once you have identified the error, you can troubleshoot it by checking your input values and the function itself for any mistakes.

3. What are some common causes of function errors?

Some common causes of function errors include entering incorrect input values, using incorrect syntax, and making mistakes in the function itself. It is also possible to encounter function errors due to limitations in the mathematical software or calculator being used.

4. Can function errors be avoided?

While it is impossible to completely avoid function errors, there are steps you can take to minimize the chances of encountering them. These include carefully checking input values, using correct syntax, and double-checking the function for any mistakes. It is also helpful to have a good understanding of the mathematical concepts involved.

5. What should I do if I am unable to solve a function error?

If you are unable to solve a function error, it is best to seek help from a math expert or consult with colleagues. They may be able to provide a fresh perspective and help you identify the error. You can also try using a different mathematical software or calculator to see if the error persists.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
988
Replies
10
Views
268
Replies
5
Views
355
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
Replies
13
Views
957
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
876
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Calculus and Beyond Homework Help
Replies
30
Views
2K
  • Calculus and Beyond Homework Help
Replies
26
Views
2K
Back
Top