Matlab, cannot turn sym to double - matlab

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

Related

`fzero` function in matlab fails to find the root and keeps on generating error

I am using MATLAB and I want to find the root of an equation F(x)-u=0. Here u=0.2861 and
F=normcdf(sqrt(lambda/t)*(t/mu-1))+exp(2*lambda/mu)*normcdf(-sqrt(lambda/t)*(t/mu+1)).
The value of lambda and mu are both 1.
I typed the following code
[x,fval] = fzero(#(t) normcdf(sqrt(lambda/t)*(t/mu-1))+exp(2*lambda/mu)*normcdf(-sqrt(lambda/t)*(t/mu+1))-u, 10);
and hope this can help me find the root. I can show mathematically that this equation has unique root. However, I keep on getting the following error
Error using erfc Input must be real and full.
Error in normcdf>localnormcdf (line 128) p(todo) = 0.5 * erfc(-z ./
sqrt(2));
Error in normcdf (line 50) [varargout{1:max(1,nargout)}] =
localnormcdf(uflag,x,varargin{:});
Error in
Test>#(t)normcdf(sqrt(lambda/t)*(t/mu-1))+exp(2*lambda/mu)*normcdf(-sqrt(lambda/t)*(t/mu+1))-u
Error in fzero (line 363)
a = x - dx; fa = FunFcn(a,varargin{:});
Then I did a "brutal force" method.
t = [0:0.001:20];
F = normcdf(sqrt(lambda./t).*(t/mu-1))+exp(2*lambda/mu).*normcdf(-sqrt(lambda./t).*(t/mu+1))-u;
plot(t,F)
I can clearly eyeball that F(t)-u is increasing in t and the root is around 0.4. My question is why fzero does not work in this case and is there a way to make fzero work?
The problem is that the function does not change sign, which is required as the docs say:
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.
I broke up your code to make it a bit clearer (at least for me).
lambda = 1;
mu = 1;
u = 1;
% break up function code
arg1 = #(t) +sqrt(lambda./t).*(t./mu-1);
arg2 = #(t) -sqrt(lambda./t).*(t./mu+1);
fnc = #(t) normcdf(arg1(t))+exp(2*lambda/mu).*normcdf(arg2(t))-u;
% call fzero to find the root
% [x,fval] = fzero(fnc, 10);
% plot
x = 0:0.01:10;
plot(x,fnc(x))
The function is not defined for any input t < 0 due to the sqrt in my function handle arg. So if you plot it for values t > 0, you see that it never passes zero.
EDITED: sign mix-up in the arguments. Thx flxx for pointing this out. Plot & code updated. The argument still holds.

Matlab - vpasolve error when solving system of equations

I'm currently trying to solve a system of equations, where h2, eta, B, U are known but I have left them as symbolic variables for now:
clearvars
clc
syms x x0 h0 h1 h2 C2 eta B U
h(x) = h0 + h2*(x/(B/2))^2;
h_bar(x) = h(x)/h0;
hc_bar = 1+(h2/h0)*(x0/(B/2))^2;
x_bar(x) = x/(B/2);
x0_bar = x_bar(x0);
integration = int(((h_bar^2-hc_bar^2)/(h_bar^3)),x);
p(x) = ((3*eta*U*B)/(h0^2))*integration+C2;
p_d(x) = diff(p(x),x);
p_fun1 = p(x0_bar) == 0;
p_fun2 = p_d(x0_bar) == 0;
p_fun3 = p(-1) == 0;
p_fun4 = h1-h0 == h2;
Four equations, four unknowns so I should be able to solve this. However the system of equations is a bit overwhelming so I decided to try my luck with vpasolve:
vpasolve([p_fun1, p_fun2, p_fun3, p_fun4],[x0, h0, h1, C2])
This results in the following error:
Error using mupadengine/feval (line 166)
Symbolic parameters are not allowed in nonpolynomial equations.
Error in sym/vpasolve (line 172)
sol = eng.feval('symobj::vpasolve',eqns,vars,X0);
Error in Yke (line 35)
vpasolve([p_fun1, p_fun2, p_fun3, p_fun4],[x0, h0, h1, C2])
I have traced the error back to p_fun1 specifically, but I cannot see why this specific function would trigger the error. It is complicated, but p_fun2 is the derivative of p_fun1 and p_fun3 is simply p_fun1 with a different value substituted in the equation, yet these do not trigger the error.
I'm pretty sure the error is caused by replacing x with x_bar in the p_fun1 = p(x0_bar) == 0 call, but I don't know why and therefore I also don't know how to solve it. Does anyone see what I'm doing wrong at the moment?
vpasolve is a numerical solver (in fact, vpa stands for variable-precision arithmetic), and you cannot have symbolic parameters in the equations.
You should thus substitute numerical values to all the equation parameters.

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.

Fitting model to data in matlab

i have some experimental data and a theoretical model which i would like to try and fit. i have made a function file with the model - the code is shown below
function [ Q,P ] = RodFit(k,C )
% Function file for the theoretical scattering from a Rod
% R = radius, L = length
R = 10; % radius in Å
L = 1000; % length in Å
Q = 0.001:0.0001:0.5;
fun = #(x) ( (2.*besselj(1,Q.*R.*sin(x)))./...
(Q.*R.*sin(x)).*...
(sin(Q.*L.*cos(x)./2))./...
(Q.*L.*cos(x)./2)...
).^2.*sin(x);
P = (integral(fun,0,pi/2,'ArrayValued',true))*k+C;
end
with Q being the x-values and P being the y-values. I can call the function fine from the matlab command line and it works fine e.g. [Q,P] = RodFit(1,0.001) gives me a result i can plot using plot(Q,P)
But i cannot figure how to best find the fit to some experimental data. Ideally, i would like to use the optimization toolbox and lsqcurvefit since i would then also be able to optimize the R and L parameters. but i do not know how to pass (x,y) data to lsqcurvefit. i have attempted it with the code below but it does not work
File = 30; % the specific observation you want to fit the model to
ydata = DataFiles{1,File}.data(:,2)';
% RAdius = linspace(10,1000,length(ydata));
% LEngth = linspace(100,10000,length(ydata));
Multiplier = linspace(1e-3,1e3,length(ydata));
Constant = linspace(0,1,length(ydata));
xdata = [Multiplier; Constant]; % RAdius; LEngth;
L = lsqcurvefit(#RodFit,[1;0],xdata,ydata);
it gives me the error message:
Error using *
Inner matrix dimensions must agree.
Error in RodFit (line 15)
P = (integral(fun,0,pi/2,'ArrayValued',true))*k+C;
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
i have tried i) making all vectors/matrices the same length and ii) tried using .* instead. nothing works and i am giving the same error message
Any kind of help would be greatly appreciated, whether it is suggestion regading what method is should use, suggestions to my code or something third.
EDIT TO ANSWER Osmoses:
A really good point but i do not think that is the problem. just checked the size of the all the vectors/matrices and they should be alright
>> size(Q)
ans =
1 1780
>> size(P)
ans =
1 1780
>> size(xdata)
ans =
2 1780
>> size([1;0.001]) - the initial guess/start point for xdata (x0)
ans =
2 1
>> size(ydata)
ans =
1 1780
UPDATE
I think i have identified the problem. the function RodFit works fine when i specify the input directly e.g. [Q,P] = RodFit(1,0.001);.
however, if i define x0 as x0 = [1,0.001] i cannot pass x0 to the function
>> x0 = [1;0.001]
x0 =
1.0000
0.0010
>> RodFit(x0);
Error using *
Inner matrix dimensions must agree.
Error in RodFit (line 15)
P = (integral(fun,0,pi/2,'ArrayValued',true))*k+C;
The same happens if i use x0 = [1,0.001]
clearly, matlab is interpreting x0 as input for k only and attempts to multiplay a vector of length(ydata) and a vector of length(x0) which obviously fails.
So my problem is that i need to code so that lsqcurvefit understands that the first column of xdata and x0 is the k variable and the second column of xdata and x0 is the C variable. According to the documentation - Passing Matrix Arguments - i should be able to pass x0 as a matrix to the solver. The solver should then also pass the xdata in the same format as x0.
Have you tried (that's sometimes the mistake) looking at the orientation of your input data (e.g. if xdata & ydata are both row/column vectors?). Other than that your code looks like it should work.
I have been able to solve some of the problems. One mistake in my code was that the objective function did not use of vector a variables but instead took in two variables - k and C. changing the code to accept a vector solved this problem
function [ Q,P ] = RodFit(X)
% Function file for the theoretical scattering from a Rod
% R = radius, L = length
% Q = 0.001:0.0001:0.5;
Q = linspace(0.11198,4.46904,1780);
fun = #(x) ( (2.*besselj(1,Q.*R.*sin(x)))./...
(Q.*R.*sin(x)).*...
(sin(Q.*L.*cos(x)./2))./...
(Q.*L.*cos(x)./2)...
).^2.*sin(x);
P = (integral(fun,0,pi/2,'ArrayValued',true))*X(1)+X(2);
with the code above, i can define x0 as x0 = [1 0.001];, and pass that into RodFit and get a result. i can also pass xdata into the function and get a result e.g. [Q,P] = RodFit(xdata(2,:));
Notice i have changed the orientation of all vectors so that they are now row-vectors and xdata has size size(xdata) = 1780 2
so i thought i had solved the problem completely but i still run into problems when i run lsqcurvefit. i get the error message
Error using RodFit
Too many input arguments.
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
i have no idea why - does anyone have any idea about why Rodfit recieves to many input arguments when i call lsqcurvefit but not when i run the function manual using xdata?

Matlab error with nlinfit

I'm attempting to get the nonlinear least squares fit of the following equation:
y = 1 / (1 + a (ln(duration)^b))
The data I wish to fit this to is presented below, as is my attempt to use the nlinfit function to solve it...
x = [1.99000000000000;3.01000000000000;4.01000000000000;5.09000000000000;5.77000000000000;6.85000000000000;7.72000000000000;8.87000000000000;9.56000000000000;];
y = [1;1;0.800000000000000;0.730000000000000;0.470000000000000;0.230000000000000;0.270000000000000;0.100000000000000;0.100000000000000;];
plot(x,y,'o','linestyle','none');
p(1) = 1;
p(2) = 1;
fun = #(p,x) 1 / (1 + p(1).*(log(x).^p(2)));
myfit = nlinfit(x,y,fun,p);
My problem seems to be with defining the fourth required input for the nlinfit function ('p' in the example above). The documentation doesn't give a clear explanation of what is needed for this to work, and I can't solve the problem based on the error messages I receive:
??? Error using ==> nlinfit at 128 MODELFUN should return a vector of fitted values the same length as Y.
Error in ==> fitting at 14 myfit = nlinfit(x,y,fun,p);
I've tried setting p to repmat(1,9,1) and repmat(1,1,9), but neither of these seem to solve my problem. Any help would be greatly appreciated!
I believe you forgot the dot to do element-wise deletion in the function:
fun = #(p,x) 1 ./ (1 + p(1).*(log(x).^p(2)));
^