# Problem with drawing a function in MATLAB

• MATLAB
• Physics_rocks
In summary, MATLAB is a popular programming language and computing environment used for scientific and mathematical applications. It is commonly used for drawing functions due to its powerful graphing capabilities and ability to handle complex calculations. To plot a function in MATLAB, the "plot" function can be used by specifying x and y values. If the function does not appear on the graph, the code and axes should be checked. Multiple functions can be plotted on the same graph using the "hold on" command, and the appearance of the plot can be customized with various options in MATLAB.
Physics_rocks
hi guys ,

I'm trying to draw the following function , however MATLAB doesn't like my code . What am I doing wrong ?

z = x + (y – 100)^2

where x = -50 : 50,
y = 0 : 200

my code :

[x,y]=meshgrid(-50:1:50,-0:1:200)
surf(x , y , x. + (y.-100)^2)

thank you

You have the dots in the wrong place
Code:
surf(x , y , x + (y-100).^2)

Hello,

It looks like you have a few syntax errors in your code. Here are a few suggestions to fix it:

1. In your code, you have a period after the x and y variables in the surf command. This is not necessary and may be causing an error. Try removing the periods and see if that helps.

2. The meshgrid function creates a grid of points, but it expects the input ranges to be in increments of 1. In your code, you have specified increments of 1 for the x values, but for the y values you have specified increments of 1 for the first two numbers (-0:1:200), but then specified increments of 200 for the last number (0:200). This may cause an error. Try using increments of 1 for all three numbers in the y range.

3. The surf function expects three inputs - the x values, the y values, and the z values. In your code, you have only specified the x and y values. You need to also calculate the z values using the equation z = x + (y - 100)^2 and then pass them as the third input to the surf function.

Taking these suggestions into account, your code should look like this:

[x,y] = meshgrid(-50:1:50,0:1:200);
z = x + (y - 100).^2;
surf(x,y,z);

I hope this helps you successfully draw your function in MATLAB. Let me know if you have any further questions or if this does not solve the issue. Keep experimenting and exploring with MATLAB - it is a powerful tool for scientific analysis and visualization. Good luck!

## What is MATLAB and why is it used for drawing functions?

MATLAB is a popular programming language and computing environment used for a variety of scientific and mathematical applications. It is often used for drawing functions because of its powerful graphing capabilities and its ability to handle complex mathematical calculations.

## How do I plot a function in MATLAB?

To plot a function in MATLAB, you can use the "plot" function and specify the x and y values for each point on the graph. For example, if you want to plot the function y = x^2, you can use the following code:
x = 0:0.1:10; % creates a vector of x values
y = x.^2; % calculates the corresponding y values
plot(x,y) % plots the function on a graph

## What should I do if my function is not showing up on the graph in MATLAB?

If your function is not showing up on the graph in MATLAB, there are a few things you can check. First, make sure that you have specified the correct x and y values for your function. Also, check that the axes of your graph are appropriately scaled to display the function. If your function is still not showing up, there may be an error in your code that is preventing it from being plotted.

## Can I plot multiple functions on the same graph in MATLAB?

Yes, you can plot multiple functions on the same graph in MATLAB by using the "hold on" command. This will allow you to plot multiple functions without erasing the previous ones. For example, if you want to plot the functions y = x^2 and y = x^3 on the same graph, you can use the following code:
x = 0:0.1:10;
y = x.^2;
plot(x,y)
hold on
y = x.^3;
plot(x,y)

## Is there a way to customize the appearance of my function plot in MATLAB?

Yes, there are many ways to customize the appearance of your function plot in MATLAB. You can change the color, style, and thickness of the line, add labels and titles, adjust the axes and gridlines, and even add a legend. MATLAB also offers a variety of built-in functions for creating different types of plots, such as scatter plots and bar graphs.

• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
739
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
307
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K