Register to reply

Basic MATLAB

by hoheiho
Tags: basic, matlab
Share this thread:
hoheiho
#1
Oct27-11, 04:46 PM
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
Phys.Org News Partner Science news on Phys.org
Flapping baby birds give clues to origin of flight
Prions can trigger 'stuck' wine fermentations, researchers find
Socially-assistive robots help kids with autism learn by providing personalized prompts
Pythagorean
#2
Oct27-11, 10:14 PM
PF Gold
Pythagorean's Avatar
P: 4,287
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.
hoheiho
#3
Oct28-11, 08:56 AM
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 :(

f95toli
#4
Oct28-11, 09:45 AM
Sci Advisor
PF Gold
f95toli's Avatar
P: 2,250
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].
hoheiho
#5
Oct28-11, 10:07 AM
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].
f95toli
#6
Oct28-11, 11:07 AM
Sci Advisor
PF Gold
f95toli's Avatar
P: 2,250
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.
hoheiho
#7
Oct28-11, 11:32 AM
P: 47
Thank you for your clear explaination :). I understand much more now
qassas
#8
Feb15-12, 02:55 AM
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?
Pythagorean
#9
Feb15-12, 03:11 AM
PF Gold
Pythagorean's Avatar
P: 4,287
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
qassas
#10
Feb15-12, 03:19 AM
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
Pythagorean
#11
Feb15-12, 03:24 AM
PF Gold
Pythagorean's Avatar
P: 4,287
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


Register to reply

Related Discussions
New to MATLAB, Need basic help Math & Science Software 3
Basic Matlab Programming Math & Science Software 2
Basic question on the FFT in matlab Engineering, Comp Sci, & Technology Homework 9
Basic MATLAB help Introductory Physics Homework 6
Basic Matlab question Math & Science Software 1