Error plot matlab - matlab

i have a list of functions:
[ x - 3^(1/2)/2, x - 4967757600021511/81129638414606681695789005144064, x + 3^(1/2)/2, x - 4160783518353059/4503599627370496, x - 1723452963400281/4503599627370496, x + 3446905926800561/9007199254740992, x + 4160783518353059/4503599627370496, x - 8566355544790271/9007199254740992, x - 2647149443198255/4503599627370496, x - 4967757600021511/81129638414606681695789005144064, x + 5294298886396509/9007199254740992, x + 8566355544790271/9007199254740992, x - 8700286382685973/9007199254740992, x - 2^(1/2)/2, x - 291404338770025/1125899906842624, x + 2331234710160199/9007199254740992, x + 2^(1/2)/2, x + 2175071595671493/2251799813685248, x - 8781369964030313/9007199254740992, x - 7042111946219083/9007199254740992, x - 3908077291623905/9007199254740992, x - 4967757600021511/81129638414606681695789005144064, x + 122127415363247/281474976710656, x + 880263993277385/1125899906842624, x + 4390684982015157/4503599627370496]
and I would like to plot the functions with this command "plot(funciones_che(1))" but when I make the plot throws me the following error:
Error using plot. A numeric or double convertible argument is expected
I have also tried x = -10: 10 and plot (x, funciones_che (1)) but I get the same error

You do not have a vector of functions. The variable funciones_che is just a long concatenated sequence of numbers. If you run size(funciones_che), you should get 1 x m*n where m is length(x) and n is the number of "functions" you have. Having a true vector of functions would actually complicate your life unnecessarily.
If you have a symbolic expression somewhere along the line, it may be a bit difficult to clear: http://www.mathworks.com/help/symbolic/clear-assumptions-and-reset-the-symbolic-engine.html. My recommendation is to reset the entire symbolic engine with all its assumptions just to be safe: reset(symengine).
MATLAB is very good at plotting matrices like the one you have, but the size has to be right:
Separate your plots with semi-colons (;) rather than commas (,):
funciones_che=[ x - 3^(1/2)/2; x - 4967757600021511/81129638414606681695789005144064; x + 3^(1/2)/2; .....]
Now size(funciones_che) will be n x m.
Transpose the matrix (to make plot interpret it correctly):
funciones_che = funciones_che';
Now size(funciones_che) will be m x n.
Plot as you wanted to: plot(funciones_che(:, 1)) for the first vector, or plot(funciones_che) to put all of them on the same plot. If you are not interested in the second version, you do not have to transpose the matrix. If you do not transpose the matrix, plot using plot(funciones_che(1, :)) instead.
Final point: you do need to initialize x, for example to -10:10 as you tried.

Related

What is f(2) in the following code?

The code (written in Octave) is:
x=1:2:5;
y=1:1:3;
z=1:0.1:1.2;
f=[x+y+z,x.^2+z;sin(x.*y.*z),cos(x)];
h=x(2)-x(1);
xFor=x(1:end-1);
dffor=(f(2:end)-f(1:end-1))/h;
f(2)
dffor
The output I get is
Hello World
ans = 0.84147
dffor = -1.07926 2.62926 -2.89423 4.44423 4.77985 -5.54500 13.59500 -12.95817
I do not understand some of the code. What does f(2) evaluate?
I actually want to get the numerical derivative of the matrix with respect to x. I thought this was the method of forward differences. Also, why am I getting a [1x11] matrix as the output for dffor, which is supposed to be the numerical differentiation matrix?
first, f is a 2D matrix in your code (size [2,6]) and I assume you meant to have a vector (size [1,12]).
dffor is indeed the forward diff. and it has 11 elements (rather than 12 as f) because it has the differences between each consequent pair of f: each element is used twice except for the first and last: (10*2 + 1 + 1)/2 = 11.
f(2) is just the second element of f which equals x(2) + y(2) + z(2)

Explain MATLAB function smooth(x,y,span,'moving')

I recently came across the Matlab smooth function used as follows:
ans = smooth(x, y, span, 'moving');
The Matlab documentation states
yy = smooth(x,y,...) additionally specifies x data. If x is not provided, methods that require x data assume x = 1:length(y). You should specify x data when it is not uniformly spaced or sorted. If x is not uniform and you do not specify method, lowess is used. If the smoothing method requires x to be sorted, the sorting occurs automatically.
However, I am unclear as to what this actually means for the 'moving' average case. Is x an index for the y data, and if so how do non-integer values of x affect the 'moving' average of y?
Moving average means each value of the yy (or ans in your case) is an average of the n closest points. https://en.wikipedia.org/wiki/Moving_average
There are several methods to calculate it - it depends on WHICH POINTS we will use. For example:
( (i-1) + (i-2) + ... + (i-n) )/n;
where n - is a span or linear filtration level.
This mean that first three points can't be calculated (there are no data for it). And sometimes result must be shifted (because really - average of the first 4 points not really corresponds to 4th elements).
So Matlab use another method:
yy(1) = y(1)
yy(2) = (y(1) + y(2) + y(3))/3
yy(3) = (y(1) + y(2) + y(3) + y(4) + y(5))/5
yy(4) = (y(2) + y(3) + y(4) + y(5) + y(6))/5
...
It's more useful.
About x and y - it is usual 2d-data: each x corresponds to each y. you can avoid setting of x, then matlab will use [1, 2, 3, ..., length(y)] sequence for this. But if you have some non-uniformly distributed data you have to set it for getting right result. So if you have non-integer values it will works correct, scaling it for x axis. Here the easiest example from my head:
Say you have data y corrupted by noise and let's assume y = [2.1, 3.2, 1.7, 4.5, 5.8, 6.9]. Let's say that you have decided to use moving average of 3 window filter to smooth y.
smoothedY1 = (2.1 + 3.2 + 1.7)/3 = 2.3333
smoothedY2 = (3.2 + 1.7 + 4.5)/3 = 3.1333
smoothedY3 = (1.7 + 4.5 + 5.8)/3 = 4.0000
smoothedY3 = (4.5 + 5.8 + 6.9)/3 = 5.7333
Pay attention to the way corrupted data is being shifted to the left by one element per iteration. Now let's use smooth() in Matlab.
y = [2.1, 3.2, 1.7, 4.5, 5.8, 6.9];
smooth(y, 3, 'moving')
The above script yields the following result
ans =
2.1000
2.3333 <----
3.1333 | (smoothed data)
4.0000 |
5.7333 <----
6.9000
To answer your original question, the "x" data is just used for sorting, but is otherwise ignored when the method is 'moving':
>> x = rand(10, 1);
>> y = (1:10)' + 0.1*randn(10,1);
>> isequal(smooth(x,y,'moving'), smooth(y,'moving'))
ans =
0
>> z = sortrows([x y], 1);
>> isequal(smooth(z(:,1),z(:,2),'moving'), smooth(z(:,2),'moving'))
ans =
1
The "x" values aren't actually taken into account for the averaging, they are just used to sort "y" by increasing index.

Function plot from points result of other function combinations

I have 2 functions declared in wxmaxima: f1(x, y) and f2(x, y). Both contain if-then-else statements and basic arithmetic operations: addition, subtraction, multiplication and division.
For example (just an example, real functions look much more complicated):
f1(x, y) := block([],
if x * y < 123 then x + y
else if x / y > 7 then x - y
);
In both functions x and y change from 0.1 to 500000.
I need a 3D plot (graph) of the following points:
(x, y, z), where f1(x, y) == f2(z, x)
Note that it's impossible to extract z out from the equation above (and get a new shiny function f3(x, y)), since f1 and f2 are too complex.
Is this something possible to achieve using any computational software?
Thanks in advance!
EDIT:
What I need is the plot for
F(x, y, z) = 0
where
F(x, y, z) = f1(x, y) - f2(z, x)
For Maxima, try implicit_plot(f1(x, y) = f2(x, y), [x, <x0>, <x1>], [y, <y0>, <y1>]) where <x0>, <x1>, <y0>, <y1> are some floating point numbers which are the range of the plot. Note that load(implicit_plot) is needed since implicit_plot is not loaded by default.
As an aside, I see that your function f1 has the form if <condition1> then ... else if <condition2> then ... and that's all. That means if both <condition1> and <condition2> are false, then the function will return false, not a number. Either you must ensure that the conditions are exhaustive, or put else ... at the end of the if so that it will return a number no matter what the input.
set = Table[{i,j,0},{i,1,10},{j,0,10}];
Gives a list of desired x and y values and use those with a replace all /.
set = set /.{a_ ,b_ ,c_} -> {a,b, f1[a,b] - f2[a,b]} (*Simplified of course*)
Set is a 2d list of lists so it needs to be flattened by 1 dimension.
set = Flatten[set,1];
ListPlot3D[set (*add plot options*)]

Graphing Polynomials in MATLAB

I need to create a polynomial of the form:
P(x) = q(1,1) + q(2,2)(x-z(1)) + q(3,3)(x-z(1))(x-z(2)) + --- + q(2n, 2n)(x-z(1))(x-z(2))...(x-z(2n)) NOTE: The indices of the equation have been shifted to accomodate MATLAB.
in MATLAB. Consult this link here specifically slides 15 and 16.
I have the matrix Q filled, so I have the diagonal, and I also have z(1:2n) filled.
I'm having a hard time figuring out a way to create a polynomial that I can graph this polynomial. I've tried to use a for loop to append each term to P(x), but it doesn't operate the way I thought it would.
So far, my code will calculate the coefficients (presented as Q(0,0) -> Q(2n+1, 2n+1) in the problem above) without a problem.
I'm having an issue with the construction of a degree n polynomial of the form described above. Plotting makes more sense now, create a vector x with evaluative values, and then run them through the polynomial "function" and plot the x vector against the resulting vector.
So I just need to create this polynomial.
I would use diag and cumprod to help you accomplish this. First use diag to extract the diagonals of your matrix Q. After, use cumprod to generate a vector of cumulative products.
How cumprod works on a vector is that for each element in the vector, the i'th element collects products from 1 up to the i'th element. As an example, if we had a vector V = [1 2 3 4 5], cumprod(V) would produce [1 2 6 24 120]. The 4th element (as an example) would be 1*2*3*4, representing the products from the 1st to the 4th element.
As such, this is the code that I would do:
qdiag = diag(Q);
xMinusZ = x - z; % Takes z and does x - z for every element in z
cumProdRes = cumprod(xMinusZ);
P = sum(qdiag .* [1;cumProdRes(1:end-1)]);
P should give you P(x) that you desired. Make sure that z is a column vector to make it compatible with the diagonals extracted from Q.
NB: I believe there is a typo in your equation. The last term of your equation (going with your convention) should have (x-z(2n-1)) and not (x-z(2n)). This is because the first term in your equation does not have z.
Here's an example. Let's suppose Q is defined
Q = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
The vector z is:
z = [4;3;2;1];
Let's evaluate the function at x = 2
Extracting the diagonals of Q should give us Q = [1;6;11;16]. Subtract x from every element of z should give us:
xMinusZ = [-2;-1;0;1];
Using the equation that you have above, we have:
P = 1 + 6*(-2) + 11*(-2)*(-1) + 16*(-2)*(-1)*(0) = 11
This is what the code should give.
What if we want to do this for more than one value of x?
As you have stated in your post, you want to evaluate this for a series of x values. As such, you need to modify the code so that it looks like this (make sure that x is a column vector):
qdiag = diag(Q);
xMinusZ = repmat(x,1,length(z)) - repmat(z',length(z),1);
cumProdRes = cumprod(xMinusZ,2);
P = sum(repmat(qdiag',length(z),1).*[ones(length(z),1) cumProdRes(:,1:end-1)],2);
P should now give you a vector of outputs, and so if you want to plot this, simply do plot(x,P);

Matrix catenation in MATLAB

I have this code in Mathematica:
nxBin = Table[{-5 sX + (i - 0.5)*step, nBin[[i]]}, {i, 1, Length[nBin]}]
and I did this in MATLAB:
a=zeros(length(nBin),1);
nxBin=zeros(length(nBin),1);
for i=1:length(nBin)
anew=a*step*(i-0.5) -5*sX;
b=zeros(length(nBin(i)),1);
nxBin(i,:)=[anew , b]
end
but MATLAB says
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> begin at 52
nxBin(i,:)=[anew , b]
Can anyone tell me why I get this error? Also, can I do this with fewer lines?
You want to catenate the n-by-1 array nBin with steps (probably x-values for a histogram). Thus, you can simply create the "x-vector" and combine them.
nxBin = [ -5*sX + ((1:length(nBin))' - 0.5) * nStep, nBin(:)]
Here's the same step-by-step
%# make a vector with values from 1 to nBin
x = 1:length(nBin);
%# transpose, since it's 1-by-n and we want n-by-1
x = x'; %'#
%# apply the modification to x
x = -5*sX + (x-0.5)*nStep;
%# catenate with nBin (the colon operator guarantees it's n-by-1
nxBin = [x, nBin(:)];
EDIT
In case you want to plot this, you can do
plot(nxBin(:,1),nxBin(:,2),'.')
or, if I guess right and it's a histogram
bar(nxBin(:,1),nxBin(:,2))