Solving a Function Error with x1,x2 Inputs

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 ==> ff
  • #1
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
 
  • #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
 

Suggested for: Solving a Function Error with x1,x2 Inputs

Replies
8
Views
2K
Replies
2
Views
836
Replies
2
Views
778
Replies
1
Views
667
Replies
2
Views
701
Replies
2
Views
1K
Replies
13
Views
1K
Back
Top