MATLab, plotting a graph with a function - matlab

I'm having trouble writing this code. So, I'm trying to make a code for
func = y*x(n) + z * x(n)
All the values are arbitrary and x(n) is the value at the position n. I need to plot a graph at each nth position. So if x(1) = 5 I plot a point at when x=1 and y=5. The issue is that I can't figure out how to make an arbitrary array and don't know how to get the answer for func when I add x(n) value at the nth position. I also am having trouble plotting a graph, but think this is because I can't figure out to use the array yet.
I'm new to MatLab.

So if I am following y & z are just constants? I think the confusion is typically this would be written something like "y = ax +bx"
Like Cris Luengo mentioned in the comments above, you should should go over some basic Matlab tutorials as this is very basic.
% y and Z are constants
y = 1;
z = 2;
%this makes x = [0,1,2,...10];
x = 0:1:10;
func = y.*x + z.*x;
plot(func)

This should do the trick:
% Define X as a range between -10 to 10 (+1 on every step)...
x = -10:10;
% Define your constants...
y = 3;
z = -1;
% Define the function...
fun = #(x) (y .* x) + (z .* x);
% Plot X on the x-axis and fun(x) on the y-axis...
% fun(x) numerically evaluates fun for the given x
plot(x,fun(x));
Refer to this page for more information about anonymous functions.

Related

How to plot and define with Matlab a function defined on different subintervals, which enters an ODE

I am trying to plot and then to use it with Matlab
in an ODE as coefficient, the function
f : [2,500] -> [0,1],
But I don't know how to write the code for the definition of the function, since it is given on different subintervals.
Below is an example that uses anonymous functions/function handles. It uses each region/condition and evaluates the boundaries numerically and stores them into variables Bounds_1 and Bounds_2. These boundaries are then used to truncate each signal by multiplying each section of the piecewise function by its corresponding condition which is a logical array. It's also good to note that this plot will almost be seen as an impulse since the bounds are really small. Alternatively, you can probably achieve the same results using the built-in piecewise() function but I think this method gives a little more insight. As i increases you'll see a plot that resembles more and more of an impulse. To plot this for multiple values or i this can be run in a for-loop.
clc;
i = 3;
Bounds_1 = [i - (1/i^2),i];
Bounds_2 = [i,i + (1/i^2)];
Bounds = [Bounds_1 Bounds_2];
Min = min(Bounds);
Max = max(Bounds);
f1 = #(x) (i^2.*x - i^3 + 1).*(Bounds_1(1) < x & x <= Bounds_1(2));
f2 = #(x) (-i^2.*x + i^3 + 1).*(Bounds_2(1) < x & x <= Bounds_2(2));
f = #(x) f1(x) + f2(x);
fplot(f);
xlim([Min-2 Max+2]);
ylim([0 1.1]);
Here is another solution. You can specify the steps along the x-axis withxstep. With xlower and xupper you can specify the range of the x-axis:
figure ; hold on;
xlabel('x'); ylabel('f(x)');
for i= 2:500
[f,x] = myfunction(0.5,i);
plot (x,f,'DisplayName',sprintf('%i',i));
end
% legend
function [f,x]=myfunction(xstep,i)
%xstep: specifies steps for the x values
% specify max range x \in = [xlower -> xupper]
xlower = -10;
xupper = 600;
x2 = (i-1/i^2): xstep: i;
f2 = i^2*x2 - i^3 + 1;
x3 = i+xstep:xstep:(1+1/i^2);
f3 = -i^2*x3 + i^3 +1;
x1 = xlower:xstep:(i-1/i^2);
f1 = 0*x1;
x4 = (i+1/i^2):xstep:xupper;
f4 = 0*x4;
f = [f1,f2,f3,f4];
x = [x1,x2,x3,x4];
end
Here is what I get
Besides, I am not exactly sure what you mean with ODE (ordinary differential equation) in f(x). For me it seems like an algebraic equation.

How to evaluate function of two variables with different x and y vectors

I've been trying to evaluate a function in matlab. I want my x vector to go from 0 to 1000 and my y vector to go from 0 to 125. They should both have a length of 101.
The equation to be evaluated is z(x,y) = ay + bx, with a=10 and b=20.
a = 10;
b = 20;
n = 101;
dx = 10; % Interval length
dy = 1.25;
x = zeros(1,n);
y = zeros(1,n);
z = zeros(n,n);
for i = 1:n;
x(i) = dx*(i-1);
y(i) = dy*(i-1);
for j = 1:n;
z(i,j) = a*dy*(j-1) + b*dx*(j-1);
end
end
I get an answer, but I don't know if I did it correctly with the indices in the nested for loop?
See MATLAB's linspace function.
a=10;
b=20;
n=101;
x=linspace(0,1000,n);
y=linspace(0,125,n);
z=a*y+b*x;
This is easier and takes care of the interval spacing for you. From the linspace documentation,
y = linspace(x1,x2,n) generates n points. The spacing between the points is (x2-x1)/(n-1).
Edit:
As others have pointed out, my solution above makes a vector, not a matrix which the OP seems to want. As #obchardon pointed out, you can use meshgrid to make that 2D grid of x and y points to generate a matrix of z. Updated approached would be:
a=10;
b=20;
n=101;
x=linspace(0,1000,n);
y=linspace(0,125,n);
[X,Y] = meshgrid(x,y);
z=a*Y+b*X;
(you may swap the order of x and y depending on if you want each variable along the row or column of z.)

How to plot a equation like ay + bx + c = 0 in matlab?

I've already searched it here but I couldn't found it the way I was looking for.
I kind managed to do it using Symbolic Math but I don't understand it quite well. For exemple, after doing that
syms y x
ezplot(-y + x + 1 == 0)
i get a nice graph, but can I use this expression later to calculate its value? like, first I want to plot -y + x + 1 == 0 and at another moment I want to solve f(3) for exemple, where f(x) = x + 1 (same equation).
I know I can write a function to do that, but as a function I don't know how to plot it. In the other way, I know how to plot using symbolic math, but I don't know how to calculate it after.
I'm writing a PLA algorithm and them I need to generate the 'a', 'b' 'c' for the equation, that why I need to know how to plot and solve in a "systematic code" way, and not typing one by one.
Thanks in advance!
The equation you gave us is a straight line, so a polynomial. The coefficients are y= -b/a*x -c/a.
% ay + bx + c = 0 reads y = -b/a*x - c/a*1
a = -1;
b = 1;
c = 1;
p = [-b/a, -c/a]; % polynomial representing your equation
% plot like this
x = linspace(-2,2, 50);
figure
plot(x, polyval(p,x)) % evaluate polynomial p at the positions x
% find the solution
roots(p) # -1
If you need or want to use ezplot, you can put the polyval-expression in an inline function and you can call ezplot with that handle:
f = #(x) polyval(p, x); % the function
ezplot(f)
Just define f to be a function of the symbolic variable x:
>> syms x
>> f = x+1;
Then you can use f as the input to ezplot:
>> ezplot(f)
which produces the graph
On the other hand, to solve the equation f(x)=0 use solve as follows:
>> solve(f)
ans =
-1
ezplot and solve can be used with string inputs as well, but the string has to be different in either case. To plot the graph:
>> ezplot('x+1');
To solve the equation:
>> solve('x+1=0')
ans =
-1

MATLAB - Using for loops find all the combinations of x^2 + y

I have two for loops like this:
for x = 1:1:15
for y = 1:1:15
values(x,y) = x^2 + y
end
end
This allows me to calculate x^2 + y for every combination of x and y if they are integers.
However, what if I want to calculate x^2 + y for decimals as well?
So something like this:
for x = 0:0.1:15
for y = 0:0.1:15
????? = x^2 + y
end
end
Could anyone help me find a method that can calculate all the possibilities of x^2 + y if x and y are decimals so cannot be used as index references anymore?
use meshgrid, matlab's built in rectangular grid in 2-D and there's no need to loop!
[y x]=meshgrid(0:0.1:15)
values=x.^2+y
to visualize this:
imagesc(values);
title('values=x^2+y'); axis square
xlabel('x'); ylabel('y'); colorbar;
axis xy;
set(gca,'XTick',1:10:151,'YTick',1:10:151);
set(gca,'XTickLabel',0:1:15,'YTickLabel',0:1:15);
EDIT:
mdgrid is also fine the only thing to note is that [y x]=meshgrid... is the same [x y]=ndgird...
Use:
[x y] = ndgrid(0:0.1:15);
values = x.^2 + y;
Issues with the other answers:
#inigo's answer will change the order of x and y compared to your initial example (by using meshgrid rather than ndgrid.
#NominSim's answer has to go to extra effort get d_x from x
#mecid's answer has to count columns and rows separately (also there is no ++ operator in MATLAB). If I was to go down #mecid's route I would use the following.
x = 0:.1:15;
y = 0:.1:15;
values = zeros(numel(x),numel(y));
for xnum = 1:numel(x)
for ynum = 1:numel(y)
values(xnum,ynum) = x(xnum)^2 + y(ynum);
end
end
Since it generated some discussion, from the documentation (within MATLAB, not in the online documentation) on the difference between meshgrid and ndgrid:
meshgrid is like ndgrid except that the order of the first two input and output arguments are switched (i.e., [X,Y,Z] = meshgrid(x,y,z) produces the same result as [Y,X,Z] = ndgrid(y,x,z)) ... meshgrid is also limited to 2D or 3D.
for x =1:0.1:15
for y=1:0.1:15
values(x*10-10, y*10-10) =x^2+y;
end
end
Why not loop on integers from 1 to 151 then calculate the decimal to be used? Then you can still use index references.
i.e.
for x = 1:1:151
for y = 1:1:151
d_x = x / 10.0 - 0.1
d_y = y / 10.0 - 0.1
values(x,y) = d_x^2 + d_y
end
end
(Forgive me if my syntax is slightly off, haven't used MATLAB in a while).

Making a function in terms of a sum from 1 to n in Matlab

I'm trying to get Matlab to take this as a function of x_1 through x_n and y_1 through y_n, where k_i and r_i are all constants.
So far my idea was to take n from the user and make two 1×n vectors called x and y, and for the x_i just pull out x(i). But I don't know how to make an arbitrary sum in MATLAB.
I also need to get the gradient of this function, which I don't know how to do either. I was thinking maybe I could make a loop and add that to the function each time, but MATLAB doesn't like that.
I don't believe a loop is necessary for this calculation. MATLAB excels at vectorized operations, so would something like this work for you?
l = 10; % how large these vectors are
k = rand(l,1); % random junk values to work with
r = rand(l,1);
x = rand(l,1);
y = rand(l,1);
vals = k(1:end-1) .* (sqrt(diff(x).^2 + diff(y).^2) - r(1:end-1)).^2;
sum(vals)
EDIT: Thanks to #Amro for correcting the formula and simplifying it with diff.
You can solve for the gradient symbolically with:
n = 10;
k = sym('k',[1 n]); % Create n variables k1, k2, ..., kn
x = sym('x',[1 n]); % Create n variables x1, x2, ..., xn
y = sym('y',[1 n]); % Create n variables y1, y2, ..., yn
r = sym('r',[1 n]); % Create n variables r1, r2, ..., rn
% Symbolically sum equation
s = sum((k(1:end-1).*sqrt((x(2:end)-x(1:end-1)).^2+(y(2:end)-y(1:end-1)).^2)-r(1:end-1)).^2)
grad_x = gradient(s,x) % Gradient with respect to x vector
grad_y = gradient(s,y) % Gradient with respect to y vector
The symbolic sum and gradients can be evaluated and converted to floating point with:
% n random data values for k, x, y, and r
K = rand(1,n);
X = rand(1,n);
Y = rand(1,n);
R = rand(1,n);
% Substitute in data for symbolic variables
S = double(subs(s,{[k,x,y,r]},{[K,X,Y,R]}))
GRAD_X = double(subs(grad_x,{[k,x,y,r]},{[K,X,Y,R]}))
GRAD_Y = double(subs(grad_y,{[k,x,y,r]},{[K,X,Y,R]}))
The gradient function is the one overloaded for symbolic variables (type help sym/gradient) or see the more detailed documentation online).
Yes, you could indeed do this with a loop, considering that x, y, k, and r are already defined.
n = length(x);
s = 0;
for j = 2 : n
s = s + k(j-1) * (sqrt((x(j) - x(j-1)).^2 + (y(j) - y(j-1)).^2) - r(j-1)).^2
end
You should derive the gradient analytically and then plug in numbers. It should not be too hard to expand these terms and then find derivatives of the resulting polynomial.
Vectorized solution is something like (I wonder why do you use sqrt().^2):
is = 2:n;
result = sum( k(is - 1) .* abs((x(is) - x(is-1)).^2 + (y(is) - y(is-1)).^2 - r(is-1)));
You can either compute gradient symbolically or rewrite this code as a function and make a standard +-eps calculation. If you need a gradient to run optimization (you code looks like a fitness function) you could use algorithms that calculate them themselves, for example, fminsearch can do this