MATLAB - turning on a function within a domain

In summary, the conversation is about creating a function in MATLAB and encountering an error while trying to set an interval of n elements to equal 1. The expert suggests using the function syntax and explains how to define a function in MATLAB. They also provide a link to the MATLAB documentation for further assistance.
  • #1
Larrytsai
228
0
Hey guys,

I am stuck trying to create a function in matlab...

x[n] = {1 0 =< n =< 4
{0 elsewhere

so far I have this...

n = [0:4];
x[n] = 1;

but I get this error:

? x[n] = 1;
|
Error: Unbalanced or unexpected parenthesis or bracket.

and I can't figure out what is wrong.

thanks for your help.
 
Physics news on Phys.org
  • #2
f you are trying to set the n-th element of x to that value, use:
x(n) = 1;

MATLAB syntax for accessing an element of an array is array_name(index) with parens, not brackets.

What I would do is something like:
Code:
function [ Y ] = X(n)
    if (0 <= n) && (n <= 4)
        Y = 1;
    else
        Y = 0;
    end
end

Edit: had some posting problems...
 
  • #3
Hey thanks for the quick reply.

I am trying to set a interval of n elements to = 1.

Can you please explain "function [ Y ] = X(n)". I don't quite understand. Sorry I am kind of slow at this, as this is my first time breathing with matlab.
 
  • #4
Okay. In MATLAB, to define a function (this has to be in a separate file, but more on this later), you use the following syntax:
Code:
[COLOR="Blue"]function[/COLOR] [output variables] = function_name(arguments)
    [COLOR="SeaGreen"]% function code[/COLOR]
[COLOR="Blue"]end[/COLOR]
Here, "function" is the key word that tells MATLAB that you are defining a function. In the brackets are the output variables, the values the function gives back after it is run. "function_name" is what you call your function; this is up to you, but the M-file for the function must be named "function_name.m".

Inside the function you put your code. In my example:
Code:
[COLOR="Blue"]function[/COLOR] [ Y ] = X(n)
    [COLOR="Blue"]if[/COLOR] (0 <= n) && (n <= 4)
        Y = 1;
    [COLOR="Blue"]else[/COLOR]
        Y = 0;
[COLOR="Blue"]    end
end[/COLOR]
There is an if block that checks to see if the value of n is between 0 and 4. If it is, based on your original post it returns the value of 1. Otherwise, it returns 0. "&&" means and. Likewise, "||" is or, and "~" is not.

The if-else block and the function block must be closed with an "end" statement.

In your main M-file, to call the function you would just enter:
Code:
[COLOR="SeaGreen"]% let's say that your input variable is a vector x 
% and you want to store the output to y:[/COLOR]
y = X(x);

Does that clear things up?
 
  • #5
Yeah I understand it now.
thats great thanks a lot for your help!
 
  • #6

1. What is MATLAB?

MATLAB is a programming language and numerical computing environment used by scientists and engineers for data analysis, visualization, and mathematical modeling.

2. How do I turn on a function in MATLAB?

To turn on a function in MATLAB, you need to first define the function and then call it using the appropriate syntax. This can vary depending on the type of function you want to use.

3. What is a domain in MATLAB?

In MATLAB, a domain refers to the set of values over which a function is defined. This can be a range of numbers, a specific interval, or a set of data points.

4. Can I turn on a function within a specific domain in MATLAB?

Yes, you can turn on a function within a specific domain in MATLAB by specifying the domain in the function definition or by using the appropriate function syntax.

5. What are some common functions used in a specific domain in MATLAB?

Some common functions used in a specific domain in MATLAB include trigonometric functions, logarithmic functions, and exponential functions. Other commonly used functions include those for data analysis, interpolation, and curve fitting within a given domain.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
810
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
941
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
882
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
955
Back
Top