How to plot the graph of double integral in Matlab? - matlab

I'm new to Matlab and would greatly appreciate if anyone could help.
I have double integral which i calculated using quad2d() function:
>> syms x y
>> ymin=#(x)x.^2
>> ymax=#(x)2*x
>> fun=#(x,y)x+y
>> quad2d(fun,0,2,ymin,ymax)
ans = 3.4667
How can i plot the graph of the integral?
Thanks in advance!

Instead of using quad2d, why not define fun as a symbolic function, perform symbolic integration and then use ezplot to plot the result?
Something along these lines:
syms x y t
fun = x + y;
I = int(fun, 0, t, x ^ 2, 2 * x);
ezplot(I)

If you want to plot the integral for different values of x limits (in the above example it is between 0 and 2) you can do something like this:
counter = 1;
for xmin = 0:10
xminv(counter) = xmin;
xmax = xmin+2;
xmaxv(counter) = xmax;
z(counter) = quad2d(fun,xmin,xmax,ymin,ymax);
counter = counter +1;
end
plot(xminv,z);
Of course you can change the range that you do the integral instead of 2 to any value you want. For example if you want your x changes from over a range of 3 instead of 2 change the xmax from xmax=xmin+2 to xmax=xmin+3.

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.

MATLab, plotting a graph with a function

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.

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: how to write a vector of a few points as a continuous function?

I have two vectors:
x = [0; 1; 2]
y = [2.0000; 0; -14.7781]
If I will plot x and y I will see three points on the xy-plane. But I want to connect those three points and get them as a continuous function:
y = f(x),
y(0) = 2;
y(1) = 0;
y(2) = -14.7781;
y(0.5) = value between 2 and 0.
For example y can be treated as a ZOH (zero order held) continuous signal.
I saw that MATLAB has a function called d2c, which converts a model from discrete to continuous time. But have no idea how to link it with the vector I have already. How to do
this with MATLAB?
OK, your latest edit improves the situation a lot.
However, you still do not demarcate the problem sufficiently.
ZOH would be as simple as
>> x = [0; 1; 2];
>> y = [2.0000; 0; -14.7781];
>> f = #(new_x) y(find(x <= new_x, 1, 'last'));
>> f(0.5)
ans =
2
However, this is not what I think you mean, as the y(0.5) = value between 2 and 0 part of your question indicates.
Perhaps you want a linearly interpolated value:
>> f = #(new_x) interp1(x,y, new_x);
>> f(0.5)
ans =
1
Or a cubic splines interpolation:
>> f = #(new_x) interp1(x,y, new_x, 'spline');
>> f(0.5)
ans =
2.5973
What I'm asking is: what model best describes your signal when the sample time would decrease to infinitesmal values?
An nth degree polynomial can have at most n-1 turning points. Thus, we can do a polynomial regression:
% Input data
yy = [2.0000; 0; -14.7781];
% Parameters
n = length(yy)-1;
x = (0:1:n).';
% Regression
p = polyfit(x,yy,n);
% Plot
f = polyval(p,x);
figure
plot(x,yy,'o',x,f,'-')

Matlab Plotting x=a?

How can I draw i.e. x = 5 line in plot at Matlab?
I plot like that:
x = (-10:.1:10);
f= 10;
plot(x, f, 'r');
of course it doesn't work. For every variable of x, y is equal to 0 except for x=10. When x = 10 y equals to everything. How to plot this?
In MATLAB, plot(X, Y) simply draws points on the graph (and connects them with lines). Note that in this form of syntax, X and Y must have the same dimensions. Therefore, to plot the line x = 5 create a vector of your desired y-coordinates, and then create matching x-coordinates, which are all equal to 5:
Y = -10:0.1:10;
X = 5 * ones(size(Y));
plot(X, Y);
A useful function from the FileExchange in hline and vline.
You could also achieve this by plotting only 2 points:
f = 5;
plot([-10 10] , [1 1]*f);
I think using line is more straightforward here than plot.
x = [-10, 10];
f = ones(size(x));
f = 5 .* f;
line(x, f);