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);
Related
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
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
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.
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)));
^
Disclaimer. I am familiar with Mathematica but not Matlab, so I apologize if this is a neophyte question.
I am getting a strange error on Matlab on the the following at using Matlab's solve command:
solve(0.2 = (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
The error is:
Error: The expression to the left of the equals sign is not a valid target for an assignment.
The equivalent Solve command in Mathematica (using the same expression) works perfectly, so I don't think my expression itself is invalid.
Moreover, I get the same error when I try to use the examples on the doc site: http://www.mathworks.com/help/symbolic/mupad_ref/solve.html
Is it a configuration problem or is there something about the syntax of the command I am misinterpreting?
Edit: I also tried with == instead of =, and I get a different error:
Undefined function or variable 'M'.
Also, as a note, I am running Matlab R2011b (7.13.0.564) 64-bit (glnxa64).
Edit2: I tried the first suggested solution with syms:
>> syms M
>> solve(0.2 == (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
Error using char
Conversion to char from logical is not possible.
Error in solve>getEqns (line 245)
vc = char(v);
Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});
Edit3: I have been able to reproduce this issue with even the simplest of equations
>> syms x
>> solve(x^2 -4 == 0, x)
Error using char
Conversion to char from logical is not possible.
Error in solve>getEqns (line 245)
vc = char(v);
Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});
Moreover, I tried the solution suggested here too: MATLAB examples are failing
Matlab's fsolve command assumes the expression is set to zero. If solving numerically, you would not do:
x=solve(2=x+1,x)
but rather:
x=fsolve(#(x) x+1-2,0)
Where the equation is already set to zero, #(x) is what you are solving for, and 0 is the initial guess. Which you must include.
Using solve symbolically, it looks like this:
syms x
val=solve(x+1-2)
Or for your system:
syms M
solve(-0.2+ (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))))
ans =
4.7161724968093348297842999805458
0.029173662296926424763929809009225
- 3.8716404782846254923900841980317 - 3.4984412176176158766571497649425*i
1.4989673987314948651159693032542 + 5.5784387926679222829321168661041*i
1.4989673987314948651159693032542 - 5.5784387926679222829321168661041*i
- 3.8716404782846254923900841980317 + 3.4984412176176158766571497649425*i
you should define M as sym, and use == instead of =
syms M
solve(0.2 == (1.4+1/2)^((1.4+1)/(2*(1.4-1)))*(M)/((1+(1.4-1)/2*M^2))^((1.4+1)/(2*(1.4-1))), M)
ans =
4.7161724968093348297842999805458
0.029173662296926424763929809009225
- 3.8716404782846254923900841980317 - 3.4984412176176158766571497649425*i
1.4989673987314948651159693032542 + 5.5784387926679222829321168661041*i
1.4989673987314948651159693032542 - 5.5784387926679222829321168661041*i
- 3.8716404782846254923900841980317 + 3.4984412176176158766571497649425*i