Solving an inequality with Matlab - matlab

I have to solve an inequality but it is too hard to do it by hand. Therefore, I would like to use Matlab. Let a = [(k-3)*sqrt(v)]/s and b = 1.08148*a^2-epsilon, where epsilon = 10^(-6). The inequality that needs to be solved is:
q(a,b) < s*sqrt(v)
where s and v are known. More precisely I want to solve the above inequality for k (which occurs in a and b). Now, the problem is that q(a,b) is the greatest real root of the quartic polynomial:
(48*a^2+16*b)*x^4 - (40*a^3+168*a*b)*x^3+(-45*a^4+225*a^2*b+72*b)*x^3+(27*a^2*b-162*a*b^2)*x+27*b^3
I tried to run this:
syms x z
av = ((x-3)*sqrt(v))/s;
Q = max(roots([48*z^2+16*(1.08148*z-eps), -40*z^3-168*z*(1.08148*z-eps), -45*z^4+225*z^2*(1.08148*z-eps)+72*(1.08148*z-eps)^2, 27*z^3*(1.08148*z-eps)-162*z*(1.08148*z-eps)^2, 27*(1.08148*z-eps)^3]));
F = compose(Q,av);
solve(F-skew*sqrt(var)<0, x)
but Matlab keeps giving the following error:
Error using sym/max (line 97)
Input arguments must be convertible
to floating-point numbers.
Error in Testt (line 13)
R = max(roots([2048,
-6912*((x-3)sqrt(var)/skew)^2,
8088((x-3)sqrt(var)/skew)^4,
-3600((x-3)sqrt(var)/skew)^6,
375((x-3)*sqrt(var)/skew)^8]));
Perhaps someone has a better idea to solve it? The best way would be if I had an explicit expression for the greatest real root q of the quartic in function of a and b. However, this explicit expression is too lengthy to use.

The error information seemed to say the max() function do not support sym x z.
Because the returned result of roots() includes x and z.But Matlab doesn't know the value of x and z,so it can't calculate the value of the roots and then can't compare them.
Maybe you should improve your algorithm.

Related

Getting conversion error while using solve function on an expression generated using for loop in MATLAB?

I am trying to find the first term 'p' of a geometric series with common ratio 1.05 in MATLAB as follows. However the solve function is giving the error as below (posted right after the code). I can't seem to figure out the reason for this error, because when I display the expression for 'sum', it is correctly showing an expression in terms of'p', but the problem arises when I try to equate that to a value, and solve for 'p'. Any insights would be appreciated! Thanks.
clear all;
clc;
t=20; %no. of terms in geometric series
sum =0;
jackpot = 1000; %sum of geometric series
%p is first term
syms p
for x=1:t
sum = sum + p*((1.05)^(x-1));
end
disp(sum);
eqn1 = sum == jackpot;
solve(eqn1,p);
Output:
(18614477322052275759*p)/562949953421312000
??? Error using ==> char
Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 169
vc = char(v);
Error in ==> solve at 67
[eqns,vars] = getEqns(varargin{:});
Error in ==> geometric_trial at 13
solve(eqn1,p);
So, I got the answer for this from a user Walter Roberson on another forum. Posting here, from his answer.
I was trying this on a version of MATLAB which is really old i.e. R2010a. In this, using 'symbolic_expression == symbolic_expression' does not set up an equation for later solving, but instead compares the two expressions for literal equality and returns a logical value immediately.
In versions that old, the easiest fix is to change
eqn1 = sum == jackpot
to
eqn1 = (sum) - (jackpot)
and let solve() deal with the implicit equality to 0.

matlab help in finding dimensions

Can anybody help me with this assignment please?
I am new to matlab, and passing this year depends on this assignment, i don't have much time to explore matlab and i already wasted alot of time trying to do this assignment in my way.
I have already wrote the equations on the paper, but transfering the equations into matlab codes is really hard for me.
All i have for now is:
syms h
l = (0.75-h.^2)/(3*sqrt((5*h.^2)/4)); %h is h_max
V_default = (h.^2/2)*l;
dv = diff(V_default); %it's max. when the derivative is max.
h1 = solve( dv ==0);
h_max = (h1>0);
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
V_max = ((h_max.^2)./(2.*l_max));
but it keep give me error "Error using ./
Matrix dimensions must agree.
Error in triangle (line 9)
V_max = ((h_max.^2)./(2.*l_max)); "
Not really helping with the assignment here, but with the Matlab syntax. In the following line:
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
you're using / that is a matrix divide. You might want to use ./ which will divide the terms element by element. If I do this
l_max = (0.75-h_max.^2) ./ (3*sqrt((h_max/2).^2+(h_max.^2)));
then your code doesn't return any error. But I have no idea if it's the correct solution of your assignment, I'll leave that to you!
In line 5, the result h1 is a vector of two values but the variable itself remains symbolic, from the Symbolic Math Toolbox. MATLAB treats such variables slightly different. For that reason, the line h_max = (h1>0) doesn't really do what you expect. As I think from this point, you are interested in one value h_max, I would convert h1 to a regular MATLAB variable and change your code to the following:
h1 = double(solve( dv ==0)); % converts symbolic to regular vectors
h_max = h1(h1>0); % filters out all negative and zero values
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
V_max = ((h_max.^2)./(2.*l_max));
EDIT.
If you still have error, it means solve( ...) returns more than 1 positive values. In this case, as suggested, use dotted operations, such as ./ but the results in l_max and V_max will not be a single value but vectors of the same size as h_max. Which means you don't have one max Volume.

Solve equation with exponential term

I have the equation 1 = ((π r2)n) / n! ∙ e(-π r2)
I want to solve it using MATLAB. Is the following the correct code for doing this? The answer isn't clear to me.
n= 500;
A= 1000000;
d= n / A;
f= factorial( n );
solve (' 1 = ( d * pi * r^2 )^n / f . exp(- d * pi * r^2) ' , 'r')
The answer I get is:
Warning: The solutions are parametrized by the symbols:
k = Z_ intersect Dom::Interval([-(PI/2 -
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n))], (PI/2 +
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n)))
> In solve at 190
ans =
(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
-(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
You have several issues with your code.
1. First, you're evaluating some parts in floating-point. This isn't always bad as long as you know the solution will be exact. However, factorial(500) overflows to Inf. In fact, for factorial, anything bigger than 170 will overflow and any input bigger than 21 is potentially inexact because the result will be larger than flintmax. This calculation should be preformed symbolically via sym/factorial:
n = sym(500);
f = factorial(n);
which returns an integer approximately equal to 1.22e1134 for f.
2. You're using a period ('.') to specify multiplication. In MuPAD, upon which most of the symbolic math functions are based, a period is shorthand for concatenation.
Additionally, as is stated in the R2015a documentation (and possibly earlier):
String inputs will be removed in a future release. Use syms to declare the variables instead, and pass them as a comma-separated list or vector.
If you had not used a string, I don't think that it would have been possible for your command to get misinterpreted and return such a confusing result. Here is how you could use solve with symbolic variables:
syms r;
n = sym(500);
A = sym(1000000);
d = n/A;
s = solve(1==(d*sym(pi)*r^2)^n/factorial(n)*exp(-d*sym(pi)*r^2),r)
which, after several minutes, returns a 1,000-by-1 vector of solutions, all of which are complex. As #BenVoigt suggests, you can try the 'Real' option for solve. However, in R2015a at least, the four solutions returned in terms of lambertw don't appear to actually be real.
A couple things to note:
MATLAB is not using the values of A, d, and f from your workspace.
f . exp is not doing at all what you wanted, which was multiplication. It's instead becoming an unknown function fexp
Passing additional options of 'Real', true to solve gets rid of most of these extraneous conditions.
You probably should avoid calling the version of solve which accepts a string, and use the Symbolic Toolbox instead (syms 'r')

Error using integral: A and B must be floating-point scalars

I want to evaluate the simple example of integral
a = max(solve(x^3 - 2*x^2 + x ==0 , x));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
and the error is
Error using integral (line 85)
A and B must be floating-point scalars.
Any tips? The lower limit of my integral must be a function, not a number.
The Matlab command solve returns symbolic result. integral accepts only numeric input. Use double to convert symbolic to numeric. As your code is written now, already max should throw an error due to symbolic input. The following works.
syms x;
a = max(double(solve(x^3 - 2*x^2 + x)));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
Output: 1.9331.
the lower limit of my integral must be a function, not a number
integral is a numeric integration routine; the limits of integration must be numeric.
Check values of a by mouse over in breakpoint or removing the ; from the end of the line so it prints a. Based on the error, a is not a scalar float. You might need another max() or double() statement to transform the vector to a single value.
Solve Help : http://www.mathworks.com/help/symbolic/solve.html
Integral Help : http://www.mathworks.com/help/ref/integral.html

Numerical integration of symbolic differentiation - MATLAB

The following is a MATLAB problem.
Suppose I define an function f(x,y).
I want to calculate the partial derivative of f with respect to y, evaluated at a specific value of y, e.g., y=6. Finally, I want to integrate this new function (which is only a function of x) over a range of x.
As an example, this is what I have tried
syms x y;
f = #(x, y) x.*y.^2;
Df = subs(diff(f,y),y,2);
Int = integral(Df , 0 , 1),
but I get the following error.
Error using integral (line 82)
First input argument must be a function
handle.
Can anyone help me in writing this code?
To solve the problem, matlabFunction was required. The solution looks like this:
syms x y
f = #(x, y) x.*y.^2;
Df = matlabFunction(subs(diff(f,y),y,2));
Int = integral(Df , 0 , 1);
Keeping it all symbolic, using sym/int:
syms x y;
f = #(x, y) x.*y.^2;
Df = diff(f,y);
s = int(Df,x,0,1)
which returns y. You can substitute 2 in for y here or earlier as you did in your question. Not that this will give you an exact answer in this case with no floating-point error, as opposed to integral which calculated the integral numerically.
When Googling for functions in Matlab, make sure to pay attention what toolbox they are in and what classes (datatypes) they support for their arguments. In some cases there are overloaded versions with the same name, but in others, you may need to look around for a different method (or devise your own).