matlab not allowed sinc(0) - matlab

I try to plot these 2 graphs on the same plot but matlab return error 'can't divide by zero' and refer me to sinc of 0.
I don't know what to do bc sinc(0)=1, I don't understand the problem.
my code:
syms x
ezplot(heaviside(x+1) - heaviside(x-1), [-2, 2])
hold
t=-2:0.1:2;
syms k
r=symsum( ((sinc(k/2)/2)*exp((1i)*k*pi*(t/2))), -1,1);
plot(t,r)
problem:
??? Error: File: aa.m Line: 6 Column: 18
Unexpected MATLAB expression.
Current plot held
??? Error using ==> mupadmex
Error in MuPAD command: Division by zero [_power];
during evaluation of 'sum::sum'
Error in ==> sym.symsum at 74
r = mupadmex('symobj::map',f.s,'symobj::symsum',x.s,a.s,b.s);
Error in ==> aa at 6
r=symsum( ((sinc(k/2)/2)*exp((1i)*k*pi*(t/2))), -1,1);

Use this alternative definition for sinc:
ssinc=#(X)(1./(gamma(1+X).*gamma(1-X)))
syms x
ezplot(heaviside(x+1) - heaviside(x-1), [-2, 2])
hold
t=-2:0.1:2;
syms k
r=symsum( ((ssinc(k/2)/2)*exp((1i)*k*pi*(t/2))), -1,1);
plot(t,r)
This code uses an alternative definition of the sinc function:
(Source: Wikipedia)

Another solution, instead of using an alternative definition with the gamma function, I added a correction to redefine the x=0 point.
The original function has a 0/0 situation, I redefined it using a correction function with correction(0)=1 and correction(1)=0 otherwise. This changes to function to 1/1 at sinc(0).
%correction(0)=1, correction(x)=0 otherwise. A little bit idiotic, but I'm unable to define this in a simpler way which is compartible to the symbolic toolbox:
correction=#(x)(((heaviside(x)-.5).^2)-.25)*-4
%redefine sinc using the original function, but use correction(x) to fix sinc(0)
ssinc=#(x)((sin(pi*x)+correction(x))./((pi*x)+correction(x)))
syms x
ezplot(heaviside(x+1) - heaviside(x-1), [-2, 2])
hold
t=-2:0.1:2;
syms k
r=symsum( ((ssinc(k/2)/2)*exp((1i)*k*pi*(t/2))), -1,1);
plot(t,r)

Related

Octave integration does not evaluate definite integral with symbolic variables

I am trying to evaluate a convolution integral using the symbolic int() function and instead of returning a useful answer, my program is returning the integral itself. Here is my code
clc; clear;
pkg load symbolic
syms t tau Wn % Declare symbolic variables
f = tau^2 * sin( Wn *(t-tau) );
convolution = int( f, tau, [0 t] ); % Specify tau as the integration variable
% and integration limits are from 0 to t
pretty(convolution)
The code runs, but does not return something useful. Instead of returning an answer, it returns this:
t
⌠
⎮ 2
⎮ τ ⋅sin(Wn⋅t - Wn⋅τ) dτ
⌡
0
i.e. the original integral with the function inside of it.
I've tried troubleshooting the problem in the following ways:
Copy/pasting example code from the Octave int() help page, this works and evaluates the definite integral
Changing the integration syntax from 0, t to [0, t] this changes nothing
Making variable f and storing the function there, instead of the function being inside of int(). This changes nothing
I know the symbolic package is working, because the example code returns the correct definite integral.
Thanks.
You can use eval in order to "evaluate" (duh) the integral. E.g. with your code above, I get:
octave:9> eval( convolution )
ans = (sym)
⎧ 2
⎪t 2⋅cos(Wn⋅t) 2
⎪── + ─────────── - ─── for Wn > -∞ ∧ Wn < ∞ ∧ Wn ≠ 0
⎨Wn 3 3
⎪ Wn Wn
⎪
⎩ 0 otherwise
Note that if you actually 'define' some of those symbols on the workspace, then these get taken into account:
octave:10> Wn = 1; % or preferably `sym('1')` ...
octave:11> eval( convolution )
ans = (sym)
2
t + 2⋅cos(t) - 2

MATLAB Unable to Store Symbolic Function with Multiple Variables

I'm trying to take the definite integral from -1 to 1 of a function with respect to x. The function has variables a, b, c, d, and x, all of which I've defined as syms variables. I'm trying to keep a, b, c, d in my final integral because I'll later be differentiating with respect to each one for an optimization problem. Here's the current code that I have:
syms f(x);
syms a b c d;
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c((sqrt(45/8))*(x^2-(1/3)))+d((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;
integral = int(f, x, [-1 1]);
disp(integral);
Similar code worked when I tried it using only variables x and y for a smaller function. However, when I try this code, I get:
Error using sym/subsindex (line 825) Invalid indexing or function
definition. Indexing must follow MATLAB indexing. Function arguments
must be symbolic variables, and function body must be sym expression.
Error in sym/subsref (line 870)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in HW11 (line 4)
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)x)-c((sqrt(45/8))(x^2-(1/3)))+d((sqrt(175/8))((x^3)-(3/5)(x))))^2;
I'm pretty new to symbolic functions and syms variables in MATLAB, why is MATLAB rejecting this code? The similar code that I tried that worked was:
syms f(x);
syms y;
f(x) = (x^2) + y;
integral = int(f, x, [0 3]);
disp(integral);
As mentioned in the comment by Adam, you probably forgot to add a multiplication operator * after the the c and d, so when you write c(...) and d(...) MATLAB treats these as indexing of an array but you cannot index arrays with symbolic variables or expressions. You need to change it to c*(...) and d*(...).
Replace:
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c((sqrt(45/8))*(x^2-(1/3)))+d((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;
With:
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c*((sqrt(45/8))*(x^2-(1/3)))+d*((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;

Matlab, cannot turn sym to double

I am trying to solve a variable in an equation (syms x), I've simplified the equation. I am trying to store the value in P_9, a 1x1000 matrix by converting from a symbol to a double and am getting the error below. It is giving me a symbol of 0x0, which is where I think my error lies.
Please help me troubleshoot my code. Many thanks!
number = 1000;
P_9 = zeros(1,number);
A_t=0.67;
A_e = linspace(0,10,number);
for n=1:number
%% find p9
syms x
eqn = x + 5 == A_t/A_e(n);
solx = solve(eqn,x);
P_9(n) = double(solx);
end
Warning: Explicit solution could not be found.
In solve at 179
In HW4 at 74
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in HW4 (line 76)
P_9(n) = double(solx);
You certainly have an equation, where x can't be isolated.
For example it is impossible to isolate x in tan(x) + x == 1. So if you try to solve this equation analyticaly, matlab will tell you that x can't be isolated and therefore there is no explicit analytical solution.
So instead of using an analytical method to solve your equation, you need to use a numerical method, it's less "sexy" but this time you will be able to solve your equation.
Life is well done, matlab already integrate a numerical solver: vpasolve.
So your code will look like:
for n=1:number
%% find p9
syms x
eqn = x + 5 == A_t/A_e(n);
solx = vpasolve(eqn,x);
P_9(n) = double(solx);
end

Matlab: Using sym in limit

Say I have the following symbolic function:
syms f(t)
f(t)=sin(t)/t
I want to get the limit using another symbolic function. I tried:
syms lim(x)
lim(x)=limit(f(t),t,x)
But when I tried to use lim(0) I got this error:
Error using symengine (line 59)
Division by zero.
Can this be fixed?
Take a look at lim(x). For some reason the limit is gone. I don't really understand what is going wrong there. If you use an anonymous function instead of a function handle, the evaluation of limit is postponed until x has a value and it works.
>> lim=#(x)limit(f(t),t,x)
lim =
#(x)limit(f(t),t,x)
>> lim(0)
ans =
1
Matlab does not have delayed assignments as discussed here. Therefore, when lim is created, the call to limit is immediately evaluated with x replacing t:
>> syms t x f(t) lim(x)
>> f(t) = sin(t)/t
f(t) =
sin(t)/t
>> lim(x) = limit(f(t),t,x)
lim(x) =
sin(x)/x
And when you evaluate lim(0), you get sin(0)/0, which throws the error.

MATLAB errors when using Lagrange Multipliers?

I have code that, when run, should correctly use Lagrange Multipliers to find the maximum/minimum of a function here:
clear all
syms x y L;
f = x^4+2*y^4;
g = x^2+5*y^2+2*y^2-10;
firstpart=jacobian(f,[x y])-L*jacobian(g,[x y]);
[Lsoln,xsoln,ysoln]=solve(firstpart,x^2+5*y^2+2*y^2-10);
subs(f,{x,y},{xsoln,ysoln})
% The coordinates that correspond with the greatest and smallest values
% above are the maximum and minimum, respectively.
However, when I run it, I get four errors:
Error using sym.getEqnsVars>checkVariables (line 92) The second
argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 62)
checkVariables(vars);
Error in solve>getEqns (line 450) [eqns, vars] =
sym.getEqnsVars(argv{:});
Error in solve (line 225) [eqns,vars,options] = getEqns(varargin{:});
Could anyone help?
You are passing two equations as individual arguments zu solve, that is not possible. You have to put both into an array
[Lsoln,xsoln,ysoln]=solve([firstpart,x^2+5*y^2+2*y^2-10] );