Effacer les filtres
Effacer les filtres

how to plot the graph with the required data

2 vues (au cours des 30 derniers jours)
jaah navi
jaah navi le 11 Fév 2019
Modifié(e) : Stephan le 12 Fév 2019
with respect to x axis i need to have the scale starting from 0 and ends till 1000.so i used the command line x=rand(1,1000)
with respect to y axis the curve needs to start increase from 0 and once it reaches 200 it needs to be constant with respect to the fixed value of 3.298*10^6.
could anyone please help me on this.

Réponse acceptée

Stephan
Stephan le 11 Fév 2019
Modifié(e) : Stephan le 11 Fév 2019
Hi,
as Madhan suggested you can use piecewise function. If you do not have access to Symbolic Math Toolbox (which is needed for piecewise), here is another approach using the basic function for piecewise polynomials mkpp, which can be used for your purposes:
% definition of the piecewise polynomial
breaks = [0 200 1000];
coeffs = [3.298e6/200 0; 0 3.298e6];
pp = mkpp(breaks,coeffs);
% calculate the values to plot
xvals = 0:1:1000;
yvals = ppval(pp,xvals);
% plot results
plot(xvals, yvals, '-r', 'LineWidth', 2)
The result appears to be what you described in your question:
Best regards
Stephan
  4 commentaires
jaah navi
jaah navi le 12 Fév 2019
The curve needs to stepwise linear when it starts from the origin point,when it reaches 2 on y axis it can be linear and then it tends to be constant when it reaches 200 on xaxis.
The stepwise linear need to be within the range(0,0) to (5,2) with respect to thegraph attached.
Could you please help me on this.
Stephan
Stephan le 12 Fév 2019
Modifié(e) : Stephan le 12 Fév 2019
The best help i can give is trying to make you understand how to work with this function. At first you should learn how polynomials are represented in Matlab. Once you understood that:
p = [1, -4, 4]
is the Matlab representation of:
y = x^2 - 4*x + 4
you will also see, that for a stepwise linear function you will need a ploynomial that has degree 1. To represent the equation for a straight line this way:
y = a*x + b --> p = [a, b]
In this representation a is the slope and b is the y-value at the starting point of the line. Remember your math class - slope was calculated by:
a = (y2 - y1) / (x2 -x1)
b = y1
So how to the first two pieces? We do it like we learned at school:
P1(x1,y1) = (0,0)
P2(x2,y2) = (5,2)
a1 = (y2 - y1) / (x2 -x1) = (2-0) / (5-0) = 0.4
b1 = 0
p1 = [a1, b1] = [0.4, 0]
We now know the coeffients for the first piece. This will be the first line in our coeff-matrix for the mkpp function. For the second piece we do the same again, with starting in point P2 and ending in point P3. Lets assume that P3 has coordinates P3(25,16):
P2(x2,y2) = (5,2)
P3(x3,y3) = (25,16)
a2 = (y3 - y2) / (x3 -x2) = (16-2) / (25-5) = 14/20 = 0.7
b2 = 2
p2 = [a2, b2] = [0.7, 2]
Now we can build the coefficient matrix:
coeffs = [p1; p2] = [0.4, 0; 0.7, 2]
The breaks we have to define is of course always those x-values where things, start, end or change. So in this case there is one break at x=[0, 5, 25]:
breaks = [0, 5, 25]
Now we are ready for the usage of the piecewise polynomial function:
pp = mkpp(breaks,coeffs)
The result is a struct containing the needed informations about the piecewise polynomial function you defined. Now you need to calculate values on it and then plot them. Lets say we want to plot a range from x= 0...25 in steps of 0.1:
xvals = 0:0.1:25;
Calculate the y-values that correspond to your x-values with the ppval function:
yvals = ppval(pp,xvals);
Last step is plotting the results using the x-values and the calculated y-values of our piecewise function:
plot(xvals, yvals)
Thats it. If you try to understand this workflow and follow it, you can define all you need by yourself. I hope this helps, because i do not know what else i could do for you in this question - unless I write the code for you, what we both do not want i guess.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by