Plotting a function with different ranges in Matlab - matlab

I would like to plot the following function:
I tried this code:
x = 0:0-1:4;
x1 = x(x<=-1);
x2 = x(x>-1 & x<=2);
x3 = x(x>2);
y1 = -3;
y2 = 1;
y3 = 4;
plot([x1 x2 x3], [y1 y2 y3])
But it is giving me the error that vectors must be the same length. How do I fix this error?

The problem is that matlab do not understand that y1=-3 means -3 for each value of x1. It needs a vector of -3s with as many elements as x1.
One way to define such a vector is the following:
x = -4:0.1:4;
x1 = x(x<=-1);
y1 = -3*ones(1,numel(x1));
So that you can plot the you want in the following way (look at it after giving it a try):
figure;hold on
x = -4:0.1:4;
x1 = x(x<=-1);
y1 = -3*ones(1,numel(x1));
plot(x1,y1)
x2 = x(x>-1 & x<=2);
y2 = ones(1,numel(x2));
plot(x2,y2)
x3 = x(x>2);
y3 = 4*ones(1,numel(x3));
plot(x3,y3)

Related

Wrong setting of pdist input data - Matlab

I wish to check the distance between points (X2, Y2) and (X1, Y1) in each row. I used the code below but I do not get the right results (66 instead of 10). How can I set the pdist to calculate it correctly?
Script:
clc;
clear all;
x1 = [0;0;1;0;9;3;4;5;6;10;11;22];
y1 = [0;1;11;10;19;13;14;15;16;10;22;12];
x2 = [1;1;2;3;4;5;6;7;8;9;10;11];
y2 = [1;1;2;3;4;5;16;17;18;19;15;18];
dTable = table(x1,y1,x2,y2);
dArray = table2array(dTable);
D = pdist(dArray,'euclidean');

Plot solution of second order equation in MATLAB

Could you please help me with the following question:
I want to solve a second order equation with two unknowns and use the results to plot an ellipse.
Here is my function:
fun = #(x) [x(1) x(2)]*V*[x(1) x(2)]'-c
V is 2x2 symmetric matrix, c is a positive constant and there are two unknowns, x1 and x2.
If I solve the equation using fsolve, I notice that the solution is very sensitive to the initial values
fsolve(fun, [1 1])
Is it possible to get the solution to this equation without providing an exact starting value, but rather a range? For example, I would like to see the possible combinations for x1, x2 \in (-4,4)
Using ezplot I obtain the desired graphical output, but not the solution of the equation.
fh= #(x1,x2) [x1 x2]*V*[x1 x2]'-c;
ezplot(fh)
axis equal
Is there a way to have both?
Thanks a lot!
you can take the XData and YData from ezplot:
c = rand;
V = rand(2);
V = V + V';
fh= #(x1,x2) [x1 x2]*V*[x1 x2]'-c;
h = ezplot(fh,[-4,4,-4,4]); % plot in range
axis equal
fun = #(x) [x(1) x(2)]*V*[x(1) x(2)]'-c;
X = fsolve(fun, [1 1]); % specific solution
hold on;
plot(x(1),x(2),'or');
% possible solutions in range
x1 = h.XData;
x2 = h.YData;
or you can use vector input to fsolve:
c = rand;
V = rand(2);
V = V + V';
x1 = linspace(-4,4,100)';
fun2 = #(x2) sum(([x1 x2]*V).*[x1 x2],2)-c;
x2 = fsolve(fun2, ones(size(x1)));
% remove invalid values
tol = 1e-2;
x2(abs(fun2(x2)) > tol) = nan;
plot(x1,x2,'.b')
However, the easiest and most straight forward approach is to rearrange the ellipse matrix form in a quadratic equation form:
k = rand;
V = rand(2);
V = V + V';
a = V(1,1);
b = V(1,2);
c = V(2,2);
% rearange terms in the form of quadratic equation:
% a*x1^2 + (2*b*x2)*x1 + (c*x2^2) = k;
% a*x1^2 + (2*b*x2)*x1 + (c*x2^2 - k) = 0;
x2 = linspace(-4,4,1000);
A = a;
B = (2*b*x2);
C = (c*x2.^2 - k);
% solve regular quadratic equation
dicriminant = B.^2 - 4*A.*C;
x1_1 = (-B - sqrt(dicriminant))./(2*A);
x1_2 = (-B + sqrt(dicriminant))./(2*A);
x1_1(dicriminant < 0) = nan;
x1_2(dicriminant < 0) = nan;
% plot
plot(x1_1,x2,'.b')
hold on
plot(x1_2,x2,'.g')
hold off

Is there a way to graphically add two curves in MatLab?

Lets say I have a sine curve and a cosine curve. Is there any way to add them graphically? Rather than doing sin + cos?
I'm plotting things that are functions of different variables (lets say one curve takes x-values from an array of integers and the other from an array of irrational numbers), so I don't know how else to add them.
Let's say you want to plot y1 = f1(x1) and y2 = f2(x2) you could do this
X = union(x1, x2);
Y1 = interp1(x1, y1, X);
Y2 = interp1(x2, y2, X);
Y = Y1 + Y2;
plot(X, Y)
So to see this in action
x1 = 2 * pi * rand(50, 1);
x2 = 2 * pi * rand(50, 1);
y1 = cos(x1).^2;
y2 = sin(x2).^2;
X = union(x1, x2);
Y1 = interp1(x1, y1, X);
Y2 = interp1(x2, y2, X);
Y = Y1 + Y2;
plot(X, Y)

MATLAB event location to find time required for full conversion of different species

I have a system of ODEs as follows:
dx1/dt = (x1,x2,x3)
dx2/dt = (x1,x2,x3)
dx3/dt = (x1,x2,x3)
The initial conditions are x1=x2=x3=0 # t=0 and the constraints are dx1/dt = 0, dx2/dt = 0, dx3/dt = 0 for x1 = 1, x2 = 1, x3 = 1 respectively. Once x1, x2, x3 reach value of 1 they should remain at 1 for further increase in t.
I need to find out (1) x1, x2, x3 at different t and (2) estimate the values of t when each of them becomes 1. I had difficulty in getting ressults for (2).
I tried to use the following event function:
function [val,stop,dir] = event(t,X)
X1 = x1;
X2 = x2;
X3 = x3;
val = [X1 -1; X2 -1; X3 -1];
stop= [1;1;1];
dir = [1;1;1];
end
It did not work. Then I tried another code to at least find t corresponding to x3 = 1 (because x3 increases slowly compared to x1 and x2).
function [val,stop,dir] = event(t,X)
X3 = x3;
val = X3 -1;
stop= 1;
dir = 1;
end
Could anyone guide me in this regard?
With regards.
Sudip
This looks like pretty straightforward to solve with the ode solvers, without the need for event. I would define a function such as:
function dX = my_ode(t,X)
for k=1:length(X)
if abs(X(k)-1) <= 1e-6 % or use whatever tolerance you want, e.g. eps
dX(k) = 0;
else
dX(k) = X(k);
end
end
I would then call the ode solver as follows:
tspan = [0 10]; % choose whatever time span you want
X_init = [0 0 0];
[T,X] = ode45(#my_ode,tspan,X_init);
plot(T,X)
Not sure why you have the Simulink tag, but that's also easily implemented in Simulink if need be.

Different colors for multiple data series with stem plot - MATLAB

x1 = 0:1:10;
x2 = 0.5:1:10;
X = [x1, x2];
Y = [(1/1.4).^x1, (2).^(-x2)];
stem(X,Y, 'filled', 'r')
What i want to do is to have the two series with different color.
How can i achieve that??
Try this :
x1 = linspace(0,10,11)';
x2 = linspace(0.5,10,11)';
X = [x1, x2];
Y = [(1/1.4).^x1, (2).^(-x2)];
h=stem(X,Y, 'filled');
set(h(1),'Color','r')
set(h(2),'Color','k')