MATLAB Converting symbolic expressing to numeric via double - matlab

I am having a bit of an issue with my code and I can't find a good way to solve it. The issue seems to lie in the usage of double to convert a symbolic expression into a numeric expression that could be plotted using double.
Here is my code:
P1 = [-1.5, -2]
P2 = [2, 2]
P3 = [-2.5, 2.5]
P4 = [2, -1]
syms x
syms y
c = 299792.458e3
r1i = sqrt((P1(1,1) - x)^2 + (P1(1,2) - y)^2)
r2i = sqrt((P2(1,1) - x)^2 + (P2(1,2) - y)^2)
t21 = -3.7294e-6
S = double(solve(t21 == (r2i-r1i)/c, y))
However, this produces the error:
Error using symengine
DOUBLE cannot convert the input expression into a double array.
Error in sym/double (line 613)
Xstr = mupadmex('symobj::double', S.s, 0);
Error (line 18)
S = double(solve(sym(t21) == (r2i-r1i)/c, y))
I've done a bit of Googling, but I can't find any other site talking about a similar issue. Would anyone be able to assist me? Thank you so much for your time and help in advanced!
This question has been marked as a duplicate, but it doesn't quite fit the other identified link because I'm trying to plot the equation. It should produce two hyperbolas. None of the methods suggested help with the plotting

Related

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

Solving an equation involving integral in Matlab

How to solve following question in Matlab.
Let α ∈ (−∞,∞).
Then how to solve: 1 + 4 {D1(α)−1} / α = 0.4615 in MATLAB?
where D1(α) is Debye function defined by:
D1(α) = 1/α ∫t/(et−1)dt
(integral from [0 α])
MATLAB won't give you an exact answer for this but you could use.
syms alpha t real
D = #(alpha)1/alpha * int(t / (exp(t)-1), t, 0, alpha);
solve(1 + 4*(D(alpha) - 1)/alpha == 0.4615, alpha)
output is
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
> In solve (line 303)
ans =
5.0770060488781283390761360077113
Edit: After the LaTeX was interpreted and posted I realized I read it wrong. I missed a pair of parenthesis. This issue has been fixed.

MATLAB fminsearch optimization error

I want to optimize x value in the following equation
formula_1=((x(1)+x(2).*exp(-TR./x(3)))./(1+cos(time_stamps).* exp(-TR./x(3))));
diff =#(x)sum((formula_1-img_vol).^2);
[pa,fval,exfl]= fminsearch(diff,startingvals,opts);
startingvals=[1,1,0.1];
opts = optimset('Display','off','TolFun',1e-9,'TolX',1e-9);
img_vol = 32x32x11 vector.
These all I am doing in the main script file. But the error comes out is
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
Error in biexp_main (line 65)
[pa,fval,exfl]=fminsearch(diff,startingvals,opts);
Kindly help me what I suppose to do??
It is rather difficult to reproduce your error since there is many information missing. (What is the exact error? What are the sizes of TR and time_stamps?).
What should help is:
change your function diff so that it returns a scalar if this is currently not the case (check with diff(startginvals)). Maybe change it to sum(sum(sum(.))).
As indicated in the comments: diff is independent from x in the current form. The 'indirect contribution' you mention does not work: it considers formula_1 and hence x as constant.
Putting this together, try:
TR = rand; %?
time_stamps = rand; %?
img_vol = rand(32,32,11);
formula_1 = #(x) (x(1)+x(2).*exp(-TR./x(3))) ./ (1+cos(time_stamps).*exp(-TR./x(3)));
diff = #(x) sum(sum(sum((formula_1(x)-img_vol).^2)));
opts = optimset('Display','off','TolFun',1e-9,'TolX',1e-9);
startingvals=[1,1,0.1];
[xOpt,diffOpt] = fminsearch(diff,startingvals,opts);

MATLAB: (Good Ol') Error in MuPAD command

I know many a question like this has been asked before, but the cases that I've seen are more complicated (i.e. I don't understand them) and the answers seem to pertain only to the specific cases.
My case is very simple (and thus broadly applicable), taken from MATLAB's very own help page:
syms x
f(x) = [x x^2; x^3 x^4];
f(2)
The output is supposed to be as follows:
ans =
[ 2, 4]
[ 8, 16]
But instead I get this error message. How come? And how do I fix it? Thanks.
If the input expression contains a symbolic variable, use the VPA
function instead.
Error in ==> sym.sym>sym.double at 936
Xstr = mupadmex('symobj::double', S.s, 0);
Error in ==> sym.sym>privformatscalar at 2678
x = double(x);
Error in ==> sym.sym>privformat at 2663
s = privformatscalar(x);
Error in ==> sym.sym>sym.subsasgn at 1433
[inds{k},refs{k}] = privformat(inds{k});
Chances are you have an older version of MATLAB (this code doesn't work for me on 2011b either). This should be equivalent:
syms x
f = [x x^2; x^3 x^4];
subs(f,2);

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)));
^