Making functions with 'inline' command in matlab

In summary, the speaker is new to MATLAB and is having trouble integrating a function using the quad command. They first define the function with the inline command and then try to integrate it, but receive an error message. They are asking for help with their syntax and want to integrate the function \int_0^{\frac{\pi}{2}} \frac{ds}{\sqrt{1-0.25\sin^2(s)}}. They also mention trying to integrate using the quad command and receiving an error message.
  • #1
nottern
3
0
hi everybody... I'm new here, and I'm here 'cause I'm having some trouble with matlab
i was trying to integrate a function, so i first defined the function with the inline command:
f=inline('1/sqrt(1-0.25*sin(x)^2)')
now MATLAB says nothing if you write the function with this command, but it makes it after, when you decide to do some operation... that happens to me when i try to integrate using the quad command, and MATLAB throws those red letters saying Error...
i may be making some syntaxis mistakes, that's why I'm asking here for some help

by the way, i want to do this integral
[itex]\int_0^{\frac{\pi}{2}} \frac{ds}{\sqrt{1-0.25\sin^2(s)}}[/itex]
thanks
 
Last edited:
Physics news on Phys.org
  • #2
nottern said:
hi everybody... I'm new here, and I'm here 'cause I'm having some trouble with matlab
i was trying to integrate a function, so i first defined the function with the inline command:
f=inline('1/sqrt(1-0.25*sin(x)^2)')
now MATLAB says nothing if you write the function with this command, but it makes it after, when you decide to do some operation... that happens to me when i try to integrate using the quad command, and MATLAB throws those red letters saying Error...
i may be making some syntaxis mistakes, that's why I'm asking here for some help

by the way, i want to do this integral
[itex]\int_0^{\frac{\pi}{2}} \frac{ds}{\sqrt{1-0.25\sin^2(s)}}[/itex]
thanks

try
f=inline('1./sqrt(1-0.25*sin(x).^2)')
 
  • #3
for your help

Hello! It seems like you are on the right track with using the inline command to define your function. However, the error you are getting when trying to integrate using the quad command is most likely due to the syntax of your function. In MATLAB, the quad command requires the function to be defined in the form of an anonymous function, which is slightly different from using the inline command.

To define an anonymous function in MATLAB, you can use the following syntax:
f = @(x) 1/sqrt(1-0.25*sin(x)^2)

This will create a function handle for your function and you can then use it with the quad command to perform the integration. So your final code would look something like this:

f = @(x) 1/sqrt(1-0.25*sin(x)^2);
integral = quad(f, 0, pi/2);

I hope this helps clarify the issue and allows you to successfully integrate your function. Happy coding!
 

1. What is the 'inline' command in MATLAB?

The 'inline' command in MATLAB is a function that allows you to create and evaluate mathematical expressions in a single line of code. It is useful for creating simple functions or for quickly evaluating complex expressions.

2. How do I create a function using the 'inline' command?

To create a function using the 'inline' command, you first need to define the input variables and the mathematical expression you want to evaluate. Then, you can use the syntax: func = inline(expression, variables) to create the function. For example, func = inline('x^2 + 2*x + 3', 'x') creates a function that evaluates the expression x^2 + 2*x + 3 with the input variable x.

3. Can I use the 'inline' command to create functions with multiple input variables?

Yes, the 'inline' command can handle multiple input variables. You can specify these variables as a comma-separated list in the second argument of the command. For example, func = inline('x*y + z', 'x, y, z') creates a function with three input variables x, y, and z.

4. How do I evaluate a function created with the 'inline' command?

To evaluate a function created with the 'inline' command, you can use the syntax func(input), where func is the name of your function and input is the value you want to evaluate the function at. For example, func(5) would evaluate the function at x = 5.

5. Can I use the 'inline' command to create functions with conditional statements?

Yes, you can use conditional statements such as if, else, and elseif within the expression of a function created with the 'inline' command. However, you will need to use the ifelse function within your expression to handle the conditional logic. For example, func = inline('ifelse(x > 0, x^2, x)', 'x') creates a function that returns x^2 if x is greater than 0, and x otherwise.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
823
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
567
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top