[Matlab] Plotting a function inside a m-file?

In summary, an m-file in Matlab is a script file that contains commands or functions written in the Matlab programming language. It can be used to perform specific tasks. To plot a function inside an m-file, the "plot" command can be used. Multiple functions can be plotted in one m-file by using the "hold on" command. The plot can be customized using various commands such as "title", "xlabel", and "ylabel", and can be saved as an image file using the "saveas" command.
  • #1
tahayassen
270
1
A3.m:

%x = 0:pi/100:2*pi;
%y = sin(2*x);
%z = 2*cos(2*x);
%plot(x,y,'k',x,z,'--b')
%title('Plot of f(x)=sin(x) and it''s derivative')
%xlabel('X-axis')
%ylabel('Y-axis')
%legend('sin(2x)','2cos(2x)')
x = -2:0.01:2
y = f2(x)
plot(x,y)

f2.m:

function F = f2(x)
F(1) = 2*(x(1)^4)-3*(x(1)^2)+x(1)-7;

I ran A3.m but it doesn't seem to create a correct plot.
 
Physics news on Phys.org
  • #2
Got my issue solved over at MATLAB central.
 

Related to [Matlab] Plotting a function inside a m-file?

1. What is a m-file in Matlab?

An m-file in Matlab is a script file that contains a series of commands or functions written in the Matlab programming language. These files can be executed to perform a specific task or set of tasks.

2. How do I plot a function inside a m-file in Matlab?

To plot a function inside a m-file in Matlab, you can use the "plot" command and specify the function you want to plot. For example, if your function is called "my_function", you would use the command "plot(my_function)" to plot it.

3. Can I plot multiple functions in one m-file in Matlab?

Yes, you can plot multiple functions in one m-file in Matlab by using the "hold on" command between each function plot. This will allow you to plot multiple functions on the same graph.

4. How can I customize the plot in an m-file in Matlab?

You can customize the plot in an m-file in Matlab by using various commands such as "title", "xlabel", "ylabel" to add labels to the graph, and "legend" to add a legend. You can also change the color, style, and type of line used for the plot.

5. Is it possible to save the plot generated by an m-file in Matlab?

Yes, you can save the plot generated by an m-file in Matlab by using the "saveas" command. This will allow you to save the plot as an image file, such as a .png or .jpg, which can be used for presentations or reports.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
240
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
779
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
626
Back
Top