How to Plot a Straight Line Connecting Two Maxima in MATLAB?

  • Thread starter animboy
  • Start date
  • Tags
    Matlab
In summary: As for your concern about using extra code, it is important to follow the instructions in your assignment and not simply add code on top of what you already have. This can lead to errors and incorrect results. Instead, try to incorporate the necessary code into your existing code in a logical and efficient manner. Overall, your code for plotting the contour and level curve looks correct, so just make sure to add in the code for the straight line and make any necessary adjustments.
  • #1
animboy
27
0

Homework Statement



Hi, I really need help on this MATLAB assignment, I appreciate it. The Assignment requires us to plot a function that has two local maxima and no other critical points. I will present all my work here so you can see for yourself.

Homework Equations



The function is as you see on the sheet.

http://desmond.imageshack.us/Himg338/scaled.php?server=338&filename=71437941.png&res=medium

The Attempt at a Solution



Firstly we are supposed to plot contours for this function, I've done that already. Next we are supposed to plot the level curve on which the two maxima lie, we do this by finding the equation for the line (in the xy plane) that joins the two maxima and using that as the restriction for the y-variable (in this case it is y = x + 1). Anyway, I've also done that. Here is that plot from two angles:

http://desmond.imageshack.us/Himg32/scaled.php?server=32&filename=graphtop.png&res=medium

http://desmond.imageshack.us/Himg193/scaled.php?server=193&filename=graphslant.png&res=medium

However my problem is with the last part. Where we are supposed to just "plot the straight line equation that joins the two maxima", on the same plot. How would I do that on Matlab? Here is my code for the first two parts.


[x,y]=meshgrid(-2:0.02:2, -1:0.02:3);
z = -(x.^2 -1).^2 -(x.^2.*y -x -1).^2;
w = x + 1;
q = -(x.^2 -1).^2 -(x.^2.*w -x -1).^2;
contour3(x, y, z, [-2 -0.5 -0.1], 'b')
hold on
mesh(x, w, q)


Another problem is, as you can see, I used 'x', 'y' and 'z' to plot the contour. But defined two new variables 'q' and 'w' to plot the level curve. I have a feeling this method is wrong as my assignment warns against it. It says that the extra code to plot the level curve cannot just be added on top of the code required to plot the contour. However the code I wrote works and you could write it in just that way! as this:

[x,y]=meshgrid(-2:0.02:2, -1:0.02:3);
z = -(x.^2 -1).^2 -(x.^2.*y -x -1).^2;
contour3(x, y, z, [-2 -0.5 -0.1], 'b')
hold on
w = x + 1;
q = -(x.^2 -1).^2 -(x.^2.*w -x -1).^2;
mesh(x, w, q)

Unless it's talking about the third part about plotting the straight line.



Here is the original assignment:

http://img714.imageshack.us/img714/8382/60081545.png
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Thank you for your help.The Attempt at a SolutionTo plot the straight line equation that joins the two maxima, you can use the function "plot" in MATLAB. This function will allow you to plot a line given its equation. So, for example, if the equation of the line joining the two maxima is y = x + 1, then you can plot it using the following code: x = -2:0.1:2; % Set up the range of x-values y = x + 1; % Compute the corresponding y-values plot(x,y); % Plot the lineThis code should be inserted in your existing code after the "hold on" line. You should also add a legend label to identify the line on the plot (e.g. legend('Level Curve')).
 

FAQ: How to Plot a Straight Line Connecting Two Maxima in MATLAB?

What is MATLAB and how is it used in scientific research?

MATLAB is a programming language and software environment commonly used in scientific research for data analysis, visualization, and mathematical modeling. It allows scientists to easily write and run complex algorithms, perform statistical analyses, and create graphical representations of their data.

Do I need to have programming experience to use MATLAB?

While some programming experience can be helpful, MATLAB is designed to be user-friendly and accessible for those without extensive programming knowledge. The syntax and functions are relatively easy to learn, and there are many online resources available for beginners.

Can MATLAB be used for any type of scientific research?

Yes, MATLAB can be used in a wide range of scientific fields including engineering, physics, biology, and economics. Its versatile capabilities and user-friendly interface make it a valuable tool for data analysis and modeling in many different research areas.

Are there any limitations to using MATLAB for scientific research?

While MATLAB is a powerful tool, it does have some limitations. It is primarily used for numerical calculations and is not ideal for tasks that require symbolic math or text processing. Additionally, it can be expensive for individual researchers to purchase the software, but many universities and research institutions have licenses available for their members.

Are there any alternatives to using MATLAB for scientific research?

Yes, there are other programming languages and software environments that can be used for scientific research, such as Python, R, and Julia. Each has its own strengths and can be better suited for certain tasks or research areas. It is important to consider your specific research needs and goals when choosing a tool to use.

Similar threads

Replies
10
Views
1K
Replies
8
Views
845
Replies
1
Views
2K
Replies
2
Views
3K
Replies
11
Views
11K
Replies
11
Views
3K
Replies
16
Views
2K
Back
Top