You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how can i plot a graph for y vs x?
10 views (last 30 days)
Show older comments
how can i plot a graph for y vs x, for this function a=-(0.1014758667.*sin (x))./(1+0.03063737048.*y.*cos(x)+0.002809.*y.^2) where a=0.5 and x=0:20:360, y=0:0.1:1?
Accepted Answer
Star Strider
on 9 Apr 2017
Not possible.
The ‘a’ function has a maximum value of 0.1.
The Code —
x=0:20:360;
y=0:0.1:1;
[X,Y] = meshgrid(x,y);
a = @(x,y) (-0.1014758667.*sin(x))./(1+0.03063737048.*y.*cos(x)+0.002809.*y.^2);
A = a(X,Y);
[R,C] = find(A == 0.5)
figure(1)
meshc(X, Y, A)
54 Comments
Star Strider
on 10 Apr 2017
My pleasure.
This is easiest using data from the contour plot function, since it is easy to find (x,y) values at a specific value of ‘z’ with it.
One of these plots should do what you want:
x=0:20:360;
y=0:0.1:1;
[X,Y] = meshgrid(x,y);
a = @(x,y) (-0.1014758667.*sin(x))./(1+0.03063737048.*y.*cos(x)+0.002809.*y.^2);
A = a(X,Y);
figure(1)
hc = contour(X, Y, A, [0.02 0.02]);
title('Contour Plot (Contour = 0.02)')
grid
figure(2)
plot(hc(1,2:end), hc(2,2:end))
title('(X,Y) Plot — All Contour Data')
grid
figure(3)
plot(hc(1,(hc(1,2:end)<11),:), hc(2,(hc(1,2:end)<11),:))
title('(X,Y) Plot — Edited Contour Data')
grid
Star Strider
on 10 Apr 2017
My pleasure.
reem123
on 10 Apr 2017
Edited: Star Strider
on 10 Apr 2017
I am sorry the function is
-0.1014758667.*y.*sin(x))./(1+0.03063737048.*y.*cos(x)+0.002809.*y.^2)=0.02
when I did the graph is not clear
Star Strider
on 10 Apr 2017
The corrected function helps significantly.
I believe I just saw the problem. Your ‘x’ variable (that you are using as arguments to your sin and cos functions) are in degrees (going from 0 to 360), while your function takes radian arguments. Changing sin to sind and cos to cosd (the ‘d’ indicates degrees) produces this plot:
x=linspace(0,360);
y=linspace(0,1);
[X,Y] = meshgrid(x,y);
a = @(x,y) (-0.1014758667.*y.*sind(x))./(1+0.03063737048.*y.*cosd(x)+0.002809.*y.^2);
A = a(X,Y);
figure(1)
hc = contour(X, Y, A, [0.02 0.02]);
title('Contour Plot (Contour = 0.02)')
grid
I believe that is what you want. It resembles some of the curves in your ‘matqq.jpg’ image.
The ‘hc’ matrix returned by the contour function are the ‘x’ (in ‘hc(1,2:end)’) and ‘y’ (in ‘hc(2,2:end)’) coordinates of the plotted curve.
reem123
on 10 Apr 2017
thank you so much, Yes I get it from your code.. my last question please that if I want to add another plot on the same graph with just changinging tha value of a..for example (a) was 0.02..I want to add also for a=0.04
Star Strider
on 10 Apr 2017
My pleasure.
To add a second contour at ‘a=0.04’, change the contour call to:
hc = contour(X, Y, A, [0.02 0.04]);
Recovering the (x,y) coordinates from ‘hc’ becomes a bit more complicated with this change. If you do not need them, you do not have to create ‘hc’. Simply do:
contour(X, Y, A, [0.02 0.04]);
Star Strider
on 10 Apr 2017
As always, my pleasure!
Star Strider
on 10 Apr 2017
Use the hold function:
b = @(x,y) -(0.1014758667.*cosd(x))./(1+0.03063737048.*y.*cosd(x)+0.002809.*y.^2);
figure(1)
contour(X, Y, A, [0.02 0.04])
hold on
contour(X, Y, b(X,Y))
hold off
title('Contour Plot')
grid
You can set the specific contours on this one as well if you want:
contour(X, Y, b(X,Y), [0.02 0.04])
reem123
on 10 Apr 2017
yes very nice thank you I get what I want
x=linspace(0,360);
y=linspace(0,1);
[X,Y] = meshgrid(x,y);
a = @(x,y) (-0.1014758667.*y.*sind(x))./(1+0.03063737048.*y.*cosd(x)+0.002809.*y.^2);
A = a(X,Y);
b = @(x,y) (-0.03664+0.100285.*y.*cosd(x))./(1+0.03063737048.*y.*cosd(x)+0.002809.*y.^2);
figure(1)
contour(X, Y, A, [0.02 0.02])
hold on
contour(X, Y, b(X,Y), [-0.12 -0.12])
hold off
title('Contour Plot')
grid
>> x=linspace(0,360);
y=linspace(0,1);
[X,Y] = meshgrid(x,y);
a = @(x,y) (-0.1014758667.*y.*sind(x))./(1+0.03063737048.*y.*cosd(x)+0.002809.*y.^2);
A = a(X,Y);
b = @(x,y) (-0.03664+0.100285.*y.*cosd(x))./(1+0.03063737048.*y.*cosd(x)+0.002809.*y.^2);
figure(1)
contour(X, Y, A, [0.02 0.02])
hold on
contour(X, Y, b(X,Y), [-0.067 -0.067])
hold off
title('Contour Plot')
grid
Star Strider
on 10 Apr 2017
My pleasure.
I ran your code to get an idea of what you are doing. If you want to see everything about it in 3D, add this plot:
figure(9)
meshc(X, Y, A)
hold on
meshc(X, Y, b(X,Y))
hold off
grid on
view([-130 20])
Star Strider
on 10 Apr 2017
My pleasure.
I will look for the Question.
reem123
on 11 Apr 2017
I am sorry, but please if you can answer my question..... If my calculated value is 0.351, and the experimental value is 0.621+-0.4, if I want to make a plot to show that my calculated value is within 1 sigma or 2 sigma of the experimental one.
Star Strider
on 11 Apr 2017
What does ‘±0.4’ represent? How did you calculate it?
Is it a standard deviation, standard error, 95% confidence interval, or something else?
How many data points (observations) were used to calculate it? This is important, because the number of observations are used to calculate the standard error, and the degrees-of-freedom for the confidence interval based on the t-statistic.
reem123
on 11 Apr 2017
Ok, what I did is that I calculated the branching ratio for a particle in one method of calculations, and my result is 6.82*10-4, one of the experimental values that I take it from Babar or Belle is (5.8+-1.3)*10^-4....what is asked from me to do is to make a graph showing that my value is within this range(1segma)or(2segma) of the experimental one.
Star Strider
on 11 Apr 2017
Please consult ‘Babar or Belle’ and find out what the ‘±’ values represent. See my previous Comment for details.
Star Strider
on 11 Apr 2017
The standard deviation is ‘sigma’, so your calculation is straightforward.
For example,
sigma1 = 6.82E-4 - (5.8 + 1.3)*1E-4
sigma2 = 6.82E-4 - (5.8 + 2*1.3)*1E-4
sigma1 =
-2.8e-05
sigma2 =
-0.000158
Since it is negative in the first calculation, it is within 1*sigma. It will of course be less than 2*sigma, so I included that calculation as well, here as an example. Add or subtract the appropriate values of ‘sigma’ depending on whether your value is greater or less than the test value.
Star Strider
on 12 Apr 2017
My pleasure.
I am not certain what you want. The Statistics and Machine Learning Toolbox tcdf or tpdf functions may work for you. You have to know the number of observations to use the t-statistics. If you don’t have them, or if you know there are more than about 30, you can use normcdf or normpdf. Also consider their inverses if you want to get the probabilities.
Star Strider
on 12 Apr 2017
Please see the functions I referred you to.
I have no idea what you want to do.
Star Strider
on 12 Apr 2017
My pleasure.
reem123
on 12 Apr 2017
I am sorry for my frequent questions... please see this code
h(1)= plot(1, 0.351, 'ko');
hold on;
h(2) = errorbar(1, 0.641, -0.4, 0.4, 'Marker', 'x');
h(3) = errorbar(1, 0.2, -0.1, 0.1, 'Marker', 'x');
axis([0.9 1.1 0 1.2]);
set(gca, 'XTick', []);
legend(h, {'Calculated', 'Experimental'});
I will do the same as the graph attached tt...but I want it on x axis..with different locations
Star Strider
on 12 Apr 2017
I don’t see the problem.
You can get the x and y values from the contour function as I previously demonstrated. Then plot them separately and plot your data and standard deviations with the errorbar function.
If you need the contour values at exactly the x values of the published data, use the interp1 function to find them.
reem123
on 13 Apr 2017
But I have a question please for more than one experimental data..I just add more values on code..but the problem that I want them on the graph separated from each other(different locations) on X axis...this is my code please
h(1)= plot(1, 0.351, 'ko');
hold on;
h(2) = errorbar(1, 0.641, -0.4, 0.4, 'Marker', 'x');
h(3) = errorbar(1, 0.2, -0.1, 0.1, 'Marker', 'x');
h(4) = errorbar(1, 0.3, -0.1, 0.1, 'Marker', 'x');
h(5) = errorbar(1, 0.5, -0.2, 0.2, 'Marker', 'x');
axis([0.9 1.1 0 1.2]);
set(gca, 'XTick', []);
legend(h, {'Calculated', 'Experimental'});
axis([0.9 1.1 0 1.2]);
set(gca, 'XTick', []);
legend(h, {'Calculated', 'Experimental'});
Star Strider
on 13 Apr 2017
You are plotting them all at ‘x=1’.
Try something like this:
x = 1 + [1 2 3 4]/50; % Use Correct ‘x’-Values
y = [0.641 0.2 0.3 0.5];
neg = [-0.4 -0.1 -0.1 -0.2];
pos = [0.4 0.1 0.1 0.2];
figure(1)
errorbar(x, y, neg, pos, 'x')
axis([0.9 1.1 0 1.2]) % Use Correct ‘axis’ Limits For ‘x’, ‘y’ & ‘error’ Ranges
Plot your data on the same plot using the hold function, and a second plot or scatter call.
Star Strider
on 13 Apr 2017
As always, my pleasure.
reem123
on 15 Apr 2017
If I want to make the same graph with the same values, but the values are on x-axis and the lines are horizontally not vertically...thank you
x = 1 + [1 2 3 4]/50; % Use Correct ‘x’-Values
y = [0.641 0.2 0.3 0.5];
neg = [-0.4 -0.1 -0.1 -0.2];
pos = [0.4 0.1 0.1 0.2];
figure(1)
errorbar(x, y, neg, pos, 'x')
axis([0.9 1.1 0 1.2])
reem123
on 15 Apr 2017
another question please..If I have an experimental value of < 3.6 with 90% Confedence Level...what does that mean? and can I add this experimental value to the same graph..with this code
x = 1 + [1 2 3 4]/50; % Use Correct ‘x’-Values
y = [0.641 0.2 0.3 0.5];
neg = [-0.4 -0.1 -0.1 -0.2];
pos = [0.4 0.1 0.1 0.2];
figure(1)
errorbar(x, y, neg, pos, 'x')
axis([0.9 1.1 0 1.2])
Star Strider
on 15 Apr 2017
The confidence limits are the probability (in this example) that the true value of the measured quantity is within those limits.
Measured quantities always have some amount of error, because the system creating the measurement may be contaminated by noise, the measuring equipment may have noise in the ‘front-end’ analogue amplifiers, and there is quantization error in the approximation of the analogue-to-digital converter. There may be other sources as well.
All the noise is modeled as being normally distributed, although some sources may have other distributions. For example quantization noise is uniformly distributed.
The t-distribution is a variation of the normal distribution, and incorporates a ‘correction’ of sorts to increase the width of the confidence interval to account for a small sample size. The t-distribution approximates the normal distribution with a sample size greater than about 30, so you can use the normal distribution with larger sample sizes without significant loss of accuracy.
This is a qualitative discussion rather than a rigorous one. For details, see any standard textbook on statistics.
The errorbar function will accept and plot whatever quantities you give it. It will plot standard deviations, standard errors, confidence limits, and anything else you want. You simply have to be certain that the calculations creating those values are correct.
reem123
on 16 Apr 2017
Ok, thank you I have two questions please: 1- If I want to make the same graph with the same values, but the values are on x-axis and the lines are horizontally not vertically 2- can can I add this experimental value ( < 3.6 with 90%CL) to the same graph
x = 1 + [1 2 3 4]/50; % Use Correct ‘x’-Values
y = [0.641 0.2 0.3 0.5];
neg = [-0.4 -0.1 -0.1 -0.2];
pos = [0.4 0.1 0.1 0.2];
figure(1)
errorbar(x, y, neg, pos, 'x')
axis([0.9 1.1 0 1.2])
Star Strider
on 16 Apr 2017
If you want to plot the error bars horizontally, use the 'horizontal' argument with the errorbar function (in R2017a and other recent releases).
See the documentation on the errorbar function for details.
Star Strider
on 16 Apr 2017
It works correctly for me (in R2017a).
I suspect the problem is with the x-axis in your plot. I guessed at the x-coordinates to illustrate the correct way to use the plot and errorbar functions.
You must define the correct x-coordinates for your data.
Star Strider
on 16 Apr 2017
I do not know the version of MATLAB you are using. (I am using R2017a.)
There are File Exchange functions that will allow you to plot horizontal error bars. However, I do not know if they work for MATLAB versions R2014b and later, when handle graphics version 2, known as ‘HG2’, was introduced.
Star Strider
on 17 Apr 2017
That seems to be correct from your description of your problem.
Star Strider
on 17 Apr 2017
As always, my pleasure!
I always learn from Answering Questions, so we all benefit.
reem123
on 18 Apr 2017
Thank you.. I wrote this code I want to draw a with b, for different values of x and y..when x=260 then y=0.15..(260,0.15) when x=200 then y=0.1..(200,0.1) and etc.
x=260, 200, 320, 230, 216, 190, 302;
y=0.15, 0.1, 0.25, 0.2, 0.34, 0.35, 1.08;
a =(-0.1014758667.*y.*sind(x))./(1+0.03063737048.*y.*cosd(x)+0.002809.*y.^2);
b =(-0.03664+0.100285.*y.*cosd(x))./(1+0.03063737048.*y.*cosd(x)+0.002809.*y.^2);
plot (a,b,'*r');
reem123
on 12 Jun 2017
Dear Star Strider ..please I have a question ..I wrote this one ..I want to graph br Vs. x
x=5.36653:0.00001:5.36701;
z=(x.^2/6.9938)-1;
w=x/1.87;
r=log((w+(w.^2-1).^0.5)./(w.^2-1).^0.5);
e=((2.*0.4202)./(1+w))+0.4202.*r;
br=((((3.209*10.^-11).*(z+w).^2).*((0.0089888512.*e).^2).*((x.^4)-13.9876.*(x.^2)).^0.5)./(2.078813*10.^-22).*(x.^2)).*1.512.*10.^-12;
figure(x,br);
Error using figure
Too many input arguments.
Star Strider
on 12 Jun 2017
Change your figure call to a figure and plot call:
figure
plot(x,br)
That worked when I ran it with the rest of your code, and produced an acceptable plot.
More Answers (0)
See Also
Categories
Find more on Errorbars in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)