Ezplot cosine function in Matlab - matlab

I'm trying to ezplot this function(f(x)= e^-2t cost t=[-20,20]), and I guess i'm missing the syntax or something.
t=[-20:20]
x= e^-2*t,cos(t)
ezplot(t,x)
but it bringing out an error

you're mixing between x-y plot and function plot, plus you have several syntax errors.
First, t is a 41 elements vector between -21 and 21.
Second, unless you predefined a variable e then e^(-2*t) will give you Undefined function or variable 'e'. error. for exponential function just use exp(-2*t).
Third, assume you fixed the syntax errors such that x = exp(-2*t).*cos(t);, then x is also a 41 elements vector, and therefore you can simply plot it using plot(t,x). ezplot (or fplot in newer versions) is used for plotting functions (rather than vectors). If you want to plot the function in the [-21,21] interval do something like:
f = #(t) exp(-2*t).*cos(t); % this is a function handle
ezplot(f,[-20 20]) % use ezplot with function handle and t interval

Related

Plotting the sum of series

I use this code and i don't know what it needs to work for my problem:
syms x k t
for t=0:10
num=((-1)^k)/k
t1=sin(8*3.1415*k*t)
S1=symsum((num*t1),k,1,2);
x=0.5-((1/3.1415)*S1);
end
Plot(x)
On the x axis I show time and on the y axis I show the function over four periods.
When I try to run the code I get the following error:
Undefined function 'symsum' for input arguments of type 'double'.
Maybe I can't use symsum with my argument type, but is there another function I can use? Sum also didn't work:
Error using sum Dimension argument must be a positive integer scalar within indexing range.
Since you want to plot x(t), you need to use plot(t,x) where t and x are vectors.
Instead of using for t=0:10, just let t=0:10 and calculate the corresponding x.
Also, the symbolic variable is just k.
syms k
t=0:10;
num=((-1)^k)/k;
t1=sin(8*3.1415*k*t);
S1=symsum((num*t1),k,1,2);
x=0.5-((1/3.1415)*S1);
plot(t,x)
It is noted that if you let t=0:10, then the sin(8*k*pi*t) will always be 0 since t is a vector of the integer from 0 to 10. The result of x(t) will be 5:
Output when t=0:10:
As you can see, the value of x(t) is very close to each other. Theoretically, they should all be 5. But there is some numerical approximation which leads to the small error.
You probably want non-integer t. Here is a output when t=0:0.1:10

Error when plotting in Octave

In Octave 4.0.2 I have defined a function S as follows:
S = #(x) (Y(k)+((Y(k+1)-Y(k))/h(k)-(2*M(k)+M(k+1))*h(k)/6)*(x-X(k))+M(k)*(k-X(k))^2/2+(M(k+1)-M(k))*(x-X(k))^3/(6*h(k)))
When I call it to evaluate a number in the interval [X(k), X(k+1)] I get the result I expect, but when I try plotting it with the command:
fplot(S, [X(k), X(k+1)]); hold on;
I get the error "error: for A^b, A must be a square matrix. Use .^ for elementwise power."
What is going on?
Alright, I figured out that when passing a function to fplot it must be able to take a vector of inputs and return a vector of outputs.

Polyval issues after using polyint

Im trying to calculate the area of randomly generated graph, which is created from randomly generated x and y values and drawn using polyfit.
clear
clc
for i=1:8
x(i)= round((12+5).*rand - 5,0)
y(i)= round((7+6).*rand -6,0)
end
p=polyfit(x,y,5);
x1=-5:0.1:12;
y1=polyval(p,x1);
plot(x,y,'o')
hold on
plot(x1,y1)
y2=(x1)*0-5
plot(x1,y2)
hold off
syms x
S1=int(x.*0-5,x,-2,7)
pp=polyint(p,x)
S2=polyval(pp,-2)-polyval(pp,7)
S=S1+S2
However, I am getting this weird error that doesnt make any sense to me.
Undefined function 'filter' for input arguments of type 'sym'.
Error in polyval (line 56)
y = filter(1,[1 -x],p);
Why doesnt it allow me to use polyval after using polyint ? Its still a polynomial..
In other words. How could I change the end of the code to calculate the definite integral of the newly formed polynomial, which is always different

zeros of a vector-valued function

Is there any function in MATLAB that can find the zeros of a vector-valued function? The commonly used function fzero is just for scalar functions and also cannot find the zeros of any scalar function such as f(x)=x^2.
Matlab's optimization toolbox has the fsolve method that states it is capable of:
Solves a problem specified by F(x) = 0 for x, where F(x) is a function that returns a vector value. x is a vector or a matrix.
Otherwise, finding zeroes of a generic vector valued function can be done by attempting to minimize the norm of the vectorial output. Lets assume your function F(x) outputs an Nx1 vector. You could attempt to find the zero by doing the following:
y = fminunc(#(x) sum(F(x).^2));
or
y = fminsearch(#(x) sum(F(x).^2));
You would then have to check if the returned y is "sufficiently close" to zero.
One last comment, the fzero function's algorithm determines the existence of roots by checking for sign changes. The [docs] explicitly say that
x = fzero(fun,x0) tries to find a point x where fun(x) = 0. This solution is where fun(x) changes sign. fzero cannot find a root of a function such as x^2.
In fact, in older versions of matlab (R2012b) the fzero's doc had a section with its limitations that said
The fzero command finds a point where the function changes sign. If the function is continuous, this is also a point where the function has a value near zero. If the function is not continuous, fzero may return values that are discontinuous points instead of zeros. For example, fzero(#tan,1) returns 1.5708, a discontinuous point in tan.
Furthermore, the fzero command defines a zero as a point where the function crosses the x-axis. Points where the function touches, but does not cross, the x-axis are not valid zeros. For example, y = x.^2 is a parabola that touches the x-axis at 0. Because the function never crosses the x-axis, however, no zero is found. For functions with no valid zeros, fzero executes until Inf, NaN, or a complex value is detected.
Maybe I misunderstand something in your question, but you can try this solution:
y = #(x) x^2;
fminbnd(y, -100, 100)
ans = -3.5527e-15
And maybe you can try solve:
syms x y
y = #(x) x^2;
solve( y==0, x);
Can't check it right now, I will edit this solution a little bit later.

plotting symfun and a self created function on same graph matlab

i am trying to plot two function in matlab, the first one is of kinf symfun:
p = symfun(0, [m]);
p(m) = p(m)+Ck(k-3)*exp(m*(k-3)*complex(0, 2*pi/25));
here Ck is another symfun and k is a variable i pre-defined.
i want to plot it in the same graph with a function i created using the function mode:
function [x1] = xt_otot_q3( t)...
i cant make the xt_otot_q3 function a symfun because it involves if statements.
- i tried to create 2 vectors sampling the two functions and plotting them together with the plot function but for some reason the 'p' function vectors gets preatty grotesque giving me wierd output...
- i tried plotting them both using ezplot function but for some reason the sampled vector i got form xt_otot_q3 shows only as a straight line at 0.
any ideas how i should plot them together? to plot the xt_otot_q3 function i must create a vector if i try to plot it directly using ezplot it gives me the following eror:
>> ezplot(xt_otot_q3, [-10 10])
Error using xt_otot_q3 (line 2)
Not enough input arguments.
thanks in advance.
If I am understanding it properly, you have two functions p, and xt_otot_q3. You want to plot them together.
syms t;
func1 = xt_otot_q3(t);
ezplot(func1, [-10 10]);
# retain current graph, for new graph
hold on;
# symbolic function p
ezplot(p, [-10 10]);
I hope it helps.