Solving a Function Error with x1,x2 Inputs

  • Context: MATLAB 
  • Thread starter Thread starter hoheiho
  • Start date Start date
  • Tags Tags
    Error Function
Click For Summary

Discussion Overview

The discussion revolves around resolving a function error in MATLAB related to defining and using inputs in a function. Participants explore how to correctly implement a function that takes multiple inputs and returns a calculated output, as well as addressing issues related to undefined variables.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant attempts to define a function in MATLAB but encounters an error due to undefined variables x1 and x2.
  • Another participant points out that using 'f1' as both a function name and a variable can lead to confusion and errors.
  • There is a suggestion that MATLAB requires defined values for variables before they can be used in functions, emphasizing that MATLAB is not a symbolic language.
  • A later reply proposes that the function should access elements of an input vector using indexing (x(1), x(2)) instead of undefined variables.
  • Participants discuss the importance of defining inputs clearly and the implications of variable scope within MATLAB functions.
  • Another participant shifts the topic to displaying an ECG signal in MATLAB, asking for procedures to achieve this.
  • Responses to the ECG inquiry suggest using the 'load' and 'plot' functions in MATLAB for displaying signals.

Areas of Agreement / Disagreement

There is no consensus on the best approach to define the function with multiple inputs, as participants present differing views on how to handle undefined variables and function definitions. The discussion about the ECG project introduces a new topic, indicating a lack of agreement on the original function error issue.

Contextual Notes

Participants express uncertainty regarding the definitions and values of variables within the MATLAB environment, highlighting the need for clarity in variable usage and function definitions.

Who May Find This Useful

This discussion may be useful for MATLAB users, particularly those working on function definitions and input handling, as well as individuals interested in signal processing and data visualization in MATLAB.

hoheiho
Messages
43
Reaction score
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
I'm not quite sure what you're doing, you should copy the exact error and tell more about with what line the error occurred, 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.
 
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 :(
 
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].
 
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].
 
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.
 
Thank you for your clear explanation :). I understand much more now
 
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?
 
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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K