I am trying to plot a ramp function in MATLAB.
I have the following function for my ramp:
function [ y ] = ramp(x)
y=zeros(size(x));
y(x>=0)=linspace(0,x(end),length(x(x>=0)))
end
But, I want it to have a similiar effect as my step function
syms x
ezplot(5*heaviside(x-1), [-10, 10])
When I use this code:
syms x
ezplot(5*ramp(x-1), [-10, 10])
When I do (x-1) it seems to throw an error that it is impossible, may I ask for some modificaitons?:
Cannot prove '0 <= x - 1' literally. To test the statement mathematically,
use isAlways.
Your step function plot works with a function of symbolic variable x. But ramp, the way you have written it, is a function that expects numerical input (a vector of x-values). This is why your attempt to pass a symbolic x to it fails. Here is a correct way to plot this function:
x = linspace(-10,10,100);
plot(x, 5*ramp(x-1))
Alternatively, you can rewrite ramp as a function of a symbolic variable:
function y = symbramp(x)
y = (x+abs(x))/2;
end
and plot it as you did with Heaviside:
syms x
ezplot(5*symbramp(x-1), [-10,10])
Related
I have a question, because this work for many functions, but I have a trouble when trying to plot the integral of a sine (I am using matlab 2010):
clear all
close all
clc
x = linspace(-10, 10, 100);
f = #(x) sin(x);
I = arrayfun(#(x) quad(f, 0, x), x);
plot(x, f(x),'r', x, I, 'b')
I expect having a -cos(x), but instead I get something with an offset of 1, why is this happening? How should fix this problem?
The Fundamental Theorem of Calculus says that the indefinite integral of a nice function f(x) is equal to the function's antiderivative F(x), which is unique up-to an additive constant. Further, a definite integral has the form:
In this form, the constant of integration will cancel out, and the integral will exactly equal the desired antiderivative only if the lower bound evaluation vanishes. However, -cos(0) does not vanish and has a value of -1. So in order to calculate the desired antiderivative F(x), the lower bound evaluation should be added to the right-hand side.
plot(x, f(x),'r', x, I+ (-cos(0)), 'b');
This is the equivalent of assigning an initial value for the solution of ODEs a la ode45.
What you're trying to do can be achieved using the following:
x = linspace(-10, 10, 100);
syms y;
f = sin(y) %function
I =int(f,y) %integration of f
plot(x, subs(f,y,x),'r', x, subs(I,y,x), 'b')
Output:-
According to Matlab documentation, q = quad(fun,a,b)
Quadrature is a numerical method used to find the area under the graph of a function, that is, to compute a definite integral.
Integral of sin(x) equals -cos(x)
Definite integral of sin(x) from x = pi to x = 0:
-cos(pi) - (-cos(0)) = 2
Since quad compute a definite integral, I can't see any problem.
Same as:
figure;plot(-cos(x) - (-cos(0)))
So, this is my script:
syms t r w
x1=5^-t*heaviside(t);
x_2=subs(x1,t,t-r);
x2=conj(x_2);
R=int(x1*x2,t,-inf,inf);
R=simplify(R)
ezplot(R, [-10 10]);
R=piecewise([0 <= r, 1/5^r/(2*log(5))], [r <= 0, 5^r/(2*log(5))])
And as you can see I am trying to plot symbolic solution with ezplot function but I get this error:
The input string must be an expression. Implicit functions of a single variable are not supported.
As I understand matlab doesn't work with piecewise functions. Is there any other way I can plot this function?
I took the approach of rewriting the piecewise expression with ineqalities:
ezplot(#(r)(0 <= r)*1/5^r/(2*log(5)) + (r <= 0)*5^r/(2*log(5)));
Not ideal with the copy-paste, but it's better than nothing.
Alternatively you can evaluate this numerically:
x = -10:0.01:10;
y = eval(subs(R,r,x));
plot(x,y)
I'm trying to create a function, that has two output arguments:
1. The calculated f(x) value
2. The gradient
But it's calling itself recursively all the time.
What am I doing wrong?
function [y, gra] = f1(x)
y = x^2
syms z
gra = gradient(f1(z))
Thanks.
edit:
Now I have this:
function [y, gra] = f1(x)
y = x^2
if nargout == 2
syms x
gra = gradient(f1(x))
end
edit 2:
I'm trying to use the function in the following:
[y, grad] = f1(5);
y_derived = grad(10);
I think this is what you want to do:
function [y, gra] = f1(x)
f=#(x) x^2;
y=f(x); %// calculate y
syms z %// initialise symbolic variable
gra=gradient(f(z),z); %// symbolic differentiation
This will return g as a symbolic function. To calculate a value, you can use subs(gra,z,123), or, if you are going to evaluate it many times, do gradFunc=matlabFunction(gra) then gradFunc(v) where v is a vector or matrix of points you want to evaluate.
That's because the argument into gradient is your function name f1(z). As such, it keeps calling f1 when your original function is also called f1, and so the function keeps calling itself until you hit a recursion limit.
I think you meant to put gradient(y) instead. Try replacing your gradient call so that it is doing:
gra = gradient(y);
In MATLAB, if I want to plot density of a variable V I have to do
[x, y] = ksdensity(V);
plot (y, x);
If I do plot(ksdensity(V)), it only plots x and not x Vs y.
Is there an easier alternative to give ksdensity() as an argument to plot() and do the same job as plot(y, x)?
You can refactor it into a function that takes in V and plots y vs x:
function h = plot_ksdensity(V, varargin)
[x, y] = ksdensity(V);
h = plot (y, x, varargin{:});
end
using varargin means you will still have access to plot options like colours. hold on will also still work because this just calls the regular plot function.
Unfortunately no. If you don't specify explicitly the outputs, a function will return always the leftmost one from output parameter list. To convince yourself about that, create the function ftest() somewhere in your MATLAB path:
function [x, y] = ftest( )
x = 1;
y = 2;
end
then call it in the Command Window without specifying the outputs
>> ftest()
ans =
1
Why use 'fplot' to plot functions when we can use 'plot'? I can't understand
With plot you have to manually define the x values and compute the corresponding y given by the function.
>> x = 0:.01:1;
>> y = sin(10*x);
>> plot(x,y,'.-')
With fplot you define the function generically, for example as an anonymous function; pass a handle to that function; and let Matlab choose the x values and compute the y values. As an example, take a difficult function:
>> f = #(x) sin(1/x);
Assume we want to plot that between 0.01 and 1:
>> lims = [.01 1];
>> fplot(f, lims, '.-')
See how Matlab does a pretty good job choosing closer x values in the left area, where the function becomes wilder.