Plotting a Function in MATLAB Using QUAD

In summary, the conversation discusses problems with plotting a specific function in MATLAB. The correct syntax for the m-file is given and a method for plotting the function is provided, along with an explanation of the variables used.
  • #1
ksekoliastis19
1
0
hello.can anyone help me to plot this function in matlab

Q=quad(@(omeg)myfun(omeg,x,t),-20,20).im not sure if this is correct.my m file function goes like that
function y= @myfun(omeg,x,t)
y=besselj(1,omeg.*x).*cos(omeg.*t).*omeg;


thanks in advance
 
Physics news on Phys.org
  • #2
Hey-

There are a few problems with the syntax you posted if you are trying to plot the function. For starters, your m-file should look like this:


function y = myfun(omeg,x,t)
y = besselj(1,omeg.*x).*cos(omeg.*t).*omeg;

end

The '@' symbol is used for function handles, and it tells MATLAB which letter is the variable. Now let's make two vectors that we can plot:

x1 = -20:0.1:20;

for i=1:1:max(size(x1))
y1(i)=myfun(x1(i),1,1);
end

plot(x1,y1,'LineWidth',1.5)

Since you have 3 inputs for the original function, we need to keep two constant and plot against the third. In this case, I let the x1 values represent omega, and I gave x and t a default value of 1. You could switch this around easily by changing where the x1(i) is in the loop.
 

Attachments

  • besselj.jpg
    besselj.jpg
    26.1 KB · Views: 420

Related to Plotting a Function in MATLAB Using QUAD

1. How do I plot a function in MATLAB using QUAD?

To plot a function using QUAD, you first need to define the function using the "function" keyword and specify the input variables. Then, use the "quad" function to integrate the function over a specified range. Finally, use the "plot" function to plot the result.

2. Can I use QUAD to plot any type of function?

Yes, QUAD can be used to plot any type of function as long as it can be integrated over a specified range. This includes polynomials, trigonometric functions, exponential functions, and more.

3. How do I specify the range for the function to be integrated?

The range can be specified by providing the lower and upper limits as input arguments to the "quad" function. For example, quad(@(x) x^2, 0, 5) would integrate the function x^2 from 0 to 5.

4. Can I plot multiple functions using QUAD?

Yes, you can plot multiple functions by using the "hold on" command before each "plot" command. This will allow you to plot multiple functions on the same graph.

5. How can I customize the plot appearance when using QUAD?

You can use various formatting options within the "plot" function to customize the appearance of your plot. These include changing the line color, style, and width, adding axis labels and titles, and adjusting the scale of the axes. Additionally, you can use the "legend" function to add a legend to your plot to label the different functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
926
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
801
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top