Plotting of different nonlinear equations in different ranges segment wise in a single graph

Can anyone help me to plot different nonlinear equations in different ranges segmentwise in a single graph? (using fsolve, for loop, if else)
For Example:
for the range 0 < x < 0.3, plot y^2 - 2*(x+y)
for the range 0.3 < x < 0.7, plot y^2 - 2*(x+y) + 4*x*y
for the range 0.7 < x < 2. plot y^2 - 2*(x+y) + 4*x*x
I think there is some mistake in the following code: (plot (A,Y).. A:X axis, Y : Y axis)
A = 0:0.01:2;
b = 0.3;
c = 0.7;
y0 = 0;
for i = numel(A)
x = A(i);
if (A(i)<=c)
if (A(i)<b)
fun = @(y) y^2 - 2*(x+y);
Y(i) = fsolve(fun,y0);
y0 = Y(i);
elseif (A(i)>b)
fun = @(y) y^2 - 2*(x+y) + 4*x*y;
Y(i) = fsolve(fun,y0);
y0 = Y(i);
end
else if (A(i)>c)
fun = @(y) y^2 - 2*(x+y) + 4*x*x;
Y(i) = fsolve(fun,y0);
y0 = Y(i);
end
end
end
plot (A,Y);

 Accepted Answer

A = 0:0.1:2;
b = 0.3;
c = 0.7;
y0 = 0;
% y = 1:10;
for i = 1: numel(A)
x = A(i);
if (A(i)<=c) & (A(i)<b)
fun = @(y) y^2 - 2*(x+y);
Y(i) = fsolve(fun,y0);
y0 = Y(i);
elseif (A(i)>b)
fun = @(y) y^2 - 2*(x+y) + 4*x*y;
Y(i) = fsolve(fun,y0);
y0 = Y(i);
end
if (A(i)>c)
fun = @(y) y^2 - 2*(x+y) + 4*x*x;
Y(i) = fsolve(fun,y0);
y0 = Y(i);
end
end
Equation solved at initial point. fsolve completed because the vector of function values at the initial point is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
Equation solved at initial point. fsolve completed because the vector of function values at the initial point is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
plot(A,Y)
%plot (A,Y);

More Answers (1)

x = 0:0.01:2;
b = 0.3;
c = 0.7;
y = zeros(size(x));
idx = x>=0 & x<=0.3;
y(idx) = 1 + sqrt(1+2*x(idx)); % or y(idx) = 1-sqrt(1+2*x(idx));
idx = x>0.3 & x<0.7;
y(idx) = (1-2*x(idx)) + sqrt(4*(x(idx)).^2-2*x(idx)+1); % or y(idx) = (1-2*x(idx))-sqrt(4*(x(idx)).^2-2*x(idx)+1);
idx = x>=0.7 & x<=2;
y(idx) = 1 + sqrt(1+2*x(idx)-4*(x(idx)).^2); % or y(idx) = 1 - sqrt(1+2*x(idx)-4*(x(idx)).^2);
% sqrt has no real
% roots in this
% interval for x
plot(x,y)
Choose the branches of the sqrt you want to plot. The argument of the sqrt in the last expression is negative and thus gives a complex root.

Categories

Community Treasure Hunt

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

Start Hunting!