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)
Related
I have 3 equations:
f = (exp(-x.^2)).*(log(x)).^2
g = exp(-x.^2)
h = (log(x)).^2
The interval is:
x = 0.05:10
I am able to correctly plot the equations but when I try to find an integral, it says that there is an error.
The code I used to find an integral is:
integral(f,0,Inf)
integral(g,0,inf)
integral(h,0,10)
The integrals for f and g are from 0 to infinity and the integral for h is from 0 to 10. None of my code to find integrals works.
You need to define f,g,h as functions like shown below. See documentation of integral(), it takes a function as its first argument. Matlab integral documentation
x = 0.05:10
f = #(x) (exp(-x.^2)).*(log(x)).^2
g = #(x) exp(-x.^2)
h = #(x) (log(x)).^2
integral(f,0,Inf) % 1.9475
integral(g,0,inf) % 0.8862
integral(h,0,10) % 26.9673
h = #(x) (log(x)).^2
This syntax is called anonymous functions, basically they are nameless functions. In above case it takes x as input and returns log(x) squared.
From now on h is a function and it can be used like this.
h(1) % will be equal 0
For more on anonymous functions refer to matlab anonymous functions guide:
Anonymous Functions
I'm new on matlab.
How can I integrate this line of code ? ?
p2= polyfit(x,y,length(x));
from= x(1);
to= x(length(x));
I need the integration of p2.
I tried a lot with the Integration function:
value = integral(p2,from,to);
but I got
Error using integral (line 82) First input argument must be a function
handle.
Error in poly_integral (line 5)
value = integral(p2,from,to);
That is because p2, in your code, is not a function. It is just a vector of coefficients. The first argument for integral needs to be handle to the function that you want to integrate.
Judging from your code, it seems that you would want to define a function that evaluates the polynomial p2. If so, you could do something like the following example:
% take an example set of x and y
x = linspace(0, pi, 1000); % uniform samples between 0 to pi
y = sin(x); % assume, for sake of example, output is sine function of input
% polynomial fit
p2 = polyfit(x,y,4); % 4th order polynomial
% Note that, in general, the order should be much smaller than length(x).
% So you probably should review this part of your code as well.
% define a function to evaluate the polynomial
fn = #(x) polyval(p2, x);
% this means: fn(x0) is same as polyval(p2, x0)
% compute integral
value = integral(fn,x(1),x(end));
You can use the polyint function to get the polynomial coefficients for exact integration of the polynomial:
p2 = polyfit(x,y,length(x));
int = diff(polyval(polyint(p2),x([1 end])));
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)))
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])
I have a vector with discrete values that I need to pass into my ODE system and I want to use ode45 command. This vector needs to be interpolated within the solver when I use it. Is there a way this can be done?
I have a coupled system of linear ODEs.
dxdt = f(t)*x + g(t)*y
dydt = g(t)*x + h(t)*y
I have three vectors f(t), g(t) and h(t) as a function of t. I need to pass them into the the solver.
I can code up a Runge-Kutta solver in C or C++. I was suggested that doing it in Matlab would be quicker. Can someone suggest a way to do this?
Sure, you can use interp1. Suppose you have vectors fvec, gvec and tvec containing respectively the values of f, g, and the time points at which these values are taken, you can define your derivative function as:
dxydt = #(t,x) [interp1(tvec, fvec, t) * x(1) + interp1(tvec, gvec, t) * x(2)
interp1(tvec, gvec, t) * x(1) + interp1(tvec, hvec, t) * x(2)];
and use it in ode45:
[T,Y] = ode45(dxydt, tspan, [x0; y0]);