How to solve symbolic equation in matlab - matlab

I have an equation in Matlab according to X parameter . I want to find the amount of X for the random amounts of F(x) .
and I tried the code below . but It gives me two different results while my equation should have just one result .
even I tried the roots(f) instead of solve(f) but it gave me an error :
??? Undefined function or method 'isfinite' for input arguments of
type 'sym'.
anybody can help me in this ?
what should I do ?
even if I have a wrong idea about solving this problem please tell me .
Thank you
function betaDistribution_2(a,b)
syms x ;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
% I have this equation and I want to find the amount of x for the random
%amounts of p
p=int(g,x,0,x);
for i=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f);
result=eval(xi);
disp(result)
end
end

Try to filter your solutions.
a=1;
b=2;
% a threshold for imagery part
SMALL=1e-9;
syms x real;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
p=int(g,x,0,x);
% return true for physically meaningfull results
filter = #(xc) abs(imag(xc))<SMALL && real(xc)>0 && subs(g, x, xc) > 0 && subs(p, x, xc)>0;
for m=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f, x);
result=eval(xi);
idx = arrayfun (filter, result);
result = result(idx);
% make sure it is OK
assert(length(result)==1);
disp(result)
end

Related

User Made Matlab Function Fails Once All Variables are Cleared

I originally asked this question yesterday and found the answer myself; however, I used the clear all command in Matlab and now the function throws an error Undefined function or variable 'y'.
I used the code from my answer
function [s1] = L_Analytic3(eqn,t0,h,numstep,y0)
%Differential Equation solver for specific inputs
% eqn is the differential equation
% t0 is start of evaluation interval
% h is stepize
% numstep is the number of steps
% y0 is the initial condition
syms y(x)
cond = y(0) == y0;
A = dsolve(eqn, cond);
s1 = A;
S1 = s1;
for x = t0 : h : h*(numstep)
subs(x);
if x == t0
S1 = subs(s1,x);
else
S1 = [subs(S1), subs(s1,vpa(x))];
end
end
end
and also put L_Analytic3(diff(y) == y,0,0.1,5,1) into the Command Window after entering clear all. I have to run a seperate code
syms y(x)
cond = y(0) == 1;
A = dsolve(diff(y) == y, cond);
before using my function in order for the function to work. Is this just because A,ans,cond,x, and y, are already defined by the 3 line code before using the function? If so, is there a way that I can use the function without having to use that 3 line code first?
When you do L_Analytic3(diff(y) == ...); you do not have variable y defined, so MATLAB complains - it has no way of knowing y is a symbol that will be defined in the function you are calling. You do not require all 3 lines of code. syms y(x) should be enough to define y and lets you use the function call you wanted.
Now, there are 2 easy ways to fix this that I see:
A script (or another function) that has syms y(x), followed by the call to L_Analytic3 the way you are doing it (which now does not need syms y(x), it has been defined already).
Give anonymous equation as the input instead, say #(x) diff(x)==x, and change a line of L_Analytic3 slightly to A = dsolve(eqn(y), cond);
Both ways work fine for this, no idea if 2nd one breaks in more complex cases. I would likely pick 1st version if you are doing symbolic stuff, and 2nd if you would like to have same function call to both numeric and symbolic functions.

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

Symbolic expression of difference equation in matlab

How can I solve following difference equation in Matlab:
A(n+1) = (1-i*p)A(n)
where p is arbitrary constant and A(0) = A0?
The answer obtained by iteration method of the equation is A(n) = A0*(1-i*p)^(n).
But how we can obtain this answer in Matlab?
This is the closest i could get. (its not possible to make condition on symbolic variable):
syms A0 C real
% assume(in(k,'integer') & k>0);
k=90;
Ak=MyFun(A0,C,k) %Output: A0*C^90
function Ak=MyFun(A0,C,k)
if k<1
Ak=A0;
else
Ak=C*MyFun(A0,C,k-1);
end
end

Matlab: Help Solving a 2nd order ODE with the derivative input being a function of time

I have been going round and round in circles trying to get Matlab to solve the resonator circuit equation with a time varying input voltage. It works just fine as long as all the in arguments of the derivative functions are scalar values.
I have gone through others questions and found answers suggesting anonymous functions or using interpolation. When I try to implement these suggestions, I get a return error that tells me I do not have enough initial conditions to match the output of ode function or the matrices being concatenated do not match..
Here is my code:
%% Define Parameters
f0=1494.72e6;
Q=80;
R=1;
L=(Q*R)/(2*pi*f0);
C=1/((2*pi*f0)^2*L);
tp=71e-9;
N=2^16;
n=(0:N-1);
TT=1e-6;
h=TT/N;
t=n*h;
start_pulse=1;
end_pulse=round(tp/h);
V=zeros(size(t));
%% Create Voltage Pulse
for ii=1:length(n);
if ii>=start_pulse && ii<=end_pulse
V(ii)=sin(2*pi*f0*t(ii));
end
end
%%
tspan=[0 TT];
x0=[0 0];
sol=ode45(#ode,tspan,x0,[],V);
int=(0:h:TT);
sint=deval(sol,int);
plot(int,sint*C);
MY ode funtion is the following:
function [ dx ] = ode( t,x,V)
f0=1494.72e6;
Q=80;
R=1;
L=(Q*R)/(2*pi*f0);
C=1/((2*pi*f0)^2*L);
dx1 = x(2);
dx2 =((-x(1)./(L*C))-(R*x(2)./L)-(V./(L*C)));
dx = [dx1; dx2];
end
As you can see, L,C,and R all are scalar values. If I replace 'V' in dx2 with '1', the program runs just fine. I need to change V to be the matrix defined above.
Any help at all would be greatly appreciated!!! Thanks in advance!!! :)

Using solve instead of vpasolve to speed up calculations in Matlab?

I am relatively new to Matlab. At the moment I am solving an equation using vpasolve. Since that needs to loop over 80 variables, it is slow.
% input variables
% ffsample = Fourier transformed reference waveform
% ffref = Fourier transformed reference waveform
len=80;
n1=1;
n2=1.95;
alpha_grad=30;
beta_grad=14.858;
% constants
tp12=2.*n1.*cos(alpha)./(n2.*cos(alpha)+n1.*cos(beta));
tp21=2.*n2.*cos(beta)./(n1.*cos(beta)+n2.*cos(alpha));
%functions with unknown n3
syms n3
gamma= asin(n2.*sin(beta)./n3);
rp23 = (n3 .* cos(beta) - n2.*cos(gamma)) ./ (n3 .* cos(beta) + n2 .* cos(gamma));
t = cputime;
res_para=zeros(len,1);
%loop to solve for n3
digits(5):
for i=1:len
res_para(i)=vpasolve(ffsample(i)./ffref(i) == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
end
e = cputime-t
%convert results to complex numbers
res_para=double(res_para);
res_para=squeeze(res_para);
res_para=res_para';
Now, I am trying to convert this into an equation which uses solve before entering the loop. As far as I understand it, I should use solve in conjunction with vpa? At the moment I am trying like the following code, but it wouldn't start:
res_p=solve(x == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
t = cputime;
res_para=zeros(len,1);
for i=1:len
x=ffsample(i)./ffref(i);
res_para=vpa(res_p(x));
end
Any help would be greatly appreciated.
Update
Thank you for your replies. I changed the digits(5) position.
And yes, digits(5) is a very low value indeed however, it speeds teh above code up by approximately 3 times.
As I said, I am relatively new to matlab and thought that solving the equation symbolically first and then just insert the variables would greatly reduce the time, but maybe I am wrong here.
Considering I use vpasolve to solve a very simple equation numerically:
function [ xval ] = example( )
syms x
for y=1:10
xval(y) = vpasolve(y == 5.*x+6, x);
end
Wouldn't it be faster if I solve the function y=5x+6 symbolically for x first and then just include the msising values in the loop? Something like this:
function [ xval ] = example2( )
syms x
% symbolic solve for x (???)
% y=5x+6 ==> x=(y-6)/5
sol=solve( y == 5.*x+6, x)
for y=1:10
xval(y) = sol(y);
end
Obviously example2 does not work.