# Basic MATLAB

by hoheiho
Tags: basic, matlab
 P: 47 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 im 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
 PF Patron P: 3,951 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.
 P: 47 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 :(
PF Patron
P: 2,140

## Basic MATLAB

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

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].
 P: 47 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].
 PF Patron Sci Advisor P: 2,140 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.
 P: 47 Thank you for your clear explaination :). I understand much more now
 P: 4 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?
 PF Patron P: 3,951 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
 P: 4 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
 PF Patron P: 3,951 if you make a new thread with an informative title, you might get better hits. That sounds a bit involved to me: https://www.google.com/search?source...+parallel+port

 Related Discussions Math & Science Software 3 Math & Science Software 2 Engineering, Comp Sci, & Technology Homework 9 Introductory Physics Homework 6 Math & Science Software 1