I would love to know why this code is not working. if anyone has any information on what is causing matlab to find so many errors, it would be greatly appreciated.
m = 1;
c = 1.5;
fun =#(x, epsilon) 1 .* (1 - (1 - cos(x))/(2.*epsilon)).^c .* cos(m.*x);
a = #(ep) acos(1-(2*ep));
lm =#(e) 1/(2.*pi) .* integral(#(x)fun(x, e), -1.*a(e), a(e));
fprintf('ball bearing at 0.6 is %4.4f', lm(0.6));
the function that I am trying to replicate is πΌπ(π) =1/2πβ«[1 β (1 β cos(π₯))/2π]^π cos(ππ₯)dx
There should be no need for the dot modifier on the multiplication to my knowledge, however Matlab was complaining that this required element-wise operations even though there are no matrices involved.
According to the documentation of integral the function to be integrated must be vectorized:
For scalar-valued problems, the function y = fun(x) must accept a vector argument, x, and return a vector result, y. This generally means that fun must use array operators instead of matrix operators. For example, use .* (times) rather than * (mtimes).
Related
i am solving a mathematical problem and i can't continue do to the error.
I tried all constant with sin^2(x) yet its the same.
clear
clc
t = 1:0.5:10;
theta = linspace(0,pi,19);
x = 2*sin(theta)
y = sin^2*(theta)*(t/4)
Error using sin
Not enough input arguments.
Error in lab2t114 (line 9)
y = sin^2*(theta)*(t/4)
sin is a function so it should be called as sin(value) which in this case is sin(theta) It may help to consider writing everything in intermediate steps:
temp = sin(theta);
y = temp.^2 ...
Once this is done you can always insert lines from previous calculations into the next line, inserting parentheses to ensure order of operations doesn't mess things up. Note in this case you don't really need the parentheses.
y = (sin(theta)).^2;
Finally, Matlab has matrix wise and element wise operations. The element wise operations start with a period '.' In Matlab you can look at, for example, help .* (element wise multiplication) and help * matrix wise calculation. For a scalar, like 2 in your example, this distinction doesn't matter. However for calculating y you need element-wise operations since theta and t are vectors (and in this case you are not looking to do matrix multiplication - I think ...)
t = 1:0.5:10;
theta = linspace(0,pi,19);
x = 2*sin(theta) %times scalar so no .* needed
sin_theta = sin(theta);
sin_theta_squared = sin_theta.^2; %element wise squaring needed since sin_theta is a vector
t_4 = t/4; %divide by scalar, this doesn't need a period
y = sin_theta_squared.*t_4; %element wise multiplication since both variables are arrays
OR
y = sin(theta).^2.*(t/4);
Also note these intermediate variables are largely for learning purposes. It is best not to write actual code like this since, in this case, the last line is a lot cleaner.
EDIT: Brief note, if you fix the sin(theta) error but not the .^ or .* errors, you would get some error like "Error using * Inner matrix dimensions must agree." - this is generally an indication that you forgot to use the element-wise operators
Here is a function handler to the function of interest:
fun = #(x) 1 / (sqrt(x) * (x + 1));
q = integral(fun, 0, inf)
But I got the following error:
Error using *
Inner matrix dimensions must agree.
How can I solve this?
You have to use . periods when defining an element-wise operation rather than matrix operations. This is needed for the integral function inputs.
Your function should be
fun = #(x) 1./(sqrt(x).*(x+1));
Giving
q = integral(fun,0,inf)
q =
3.1416
This overview article on Array vs. Matrix operations may be of interest, outlining all element-wise . type functions.
https://uk.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
Specifically, see these help articles on multplication and right-array division to give you an idea: https://www.mathworks.com/help/matlab/ref/times.html, https://www.mathworks.com/help/matlab/ref/rdivide.html
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')
I'm trying to execute a custom function within MatLab, such that x which is a vector is actually the exponent of a constant e. I've tried placing the dot in numerous places, but it keeps throwing an error which is telling me to use the . before the ^, which is not what I want to do on this occasion. Y needs to return a vector and not a constant.
x = [0:0.1:1];
function y = hyperT(x)
e = exp(1);
y = ((e^x*2)-1)/((e^x*2)+1);
end
I've taken the . out for the purpose of this thread.
To get a vector, you can use (MATLAB power operator):
y = ((e.^x*2) - 1) ./ (e.^(x*2) + 1);
The . operator basically means "element-wise". For example, if you are multiplying two vectors, x = [x1, x2, x3] and y = [y1, y2, y3], then using the .* operator multiplies each element by the corresponding element in the other vector, while using the * operator without the . performs an inner product (matrix multiplication):
x.*y = [x1y1, x2y2, x3y3]
x*y = error (inner matrix dimensions must agree)
x'*y = [x1y1, x2y1, x3y1;
x2y1, x2y2, x3y2;
x1y3, x2y3, x3y3]
x*y' = x1y1 + x2y2 + x3y3
Note that the ' in the above transposes the vector.
Some operators automatically broadcast because their use is un-ambiguous. Thus you do not need the . with the + and - operators. Oddly enough, the division operator does not auto-broadcast, so you need to use ./ there (MATLAB rdivide). I am not sure what it is doing when you omit the ., but it seems to be well defined and consistent at least.
In general, you can perform any operation (even one that you define) element-wise between two vectors, or between a constant and a vector, by using bsxfun (MATLAB bsxfun). For example, to perform the power operation that you are asking about, you could do:
bsxfun(#power, e, x)
or
bsxfun(#power, e, x*2)
This is actually a super-efficient way to do a lot of neat things, but in your case the functionality is already built in with .^.
Edit: Added some links
you may use:
expm(x) which is matrix exponential.
I am writing a program in Matlab and I have a function defined this way.
sum (i=1...100) (a*x(i) + b*y(i) + c)
x and y are known, while a, b and c are not: I need to find values for them such that the total value of the function is minimized. There is no additional constraint for the problem.
I thought of using fminsearch to solve this minimization problem, but from Mathworks I get that functions which are suitable inputs for fminsearch are defined like this (an example):
square = #(x) x.^2
So in my case I could use a vector p=[a, b, c] as the value to minimize, but then I don't know how to define the remaining part of the function. As you can see the number of possible values for the index i is huge, so I cannot simply sum everything together explicitly, but I need to represent the summation in some way. If I write the function somewhere else then I am forced to use symbolic calculus for a, b and c (declaring them with syms) and I'm not sure fminsearch would accept that.
What can I do? Of course if fminsearch turns out to be unfeasible for my situation I accept links to use something else.
The most general solution is to use x and y in the definition of the objective function:
>> objfun = #(p) sum( p(1).*x + p(2).*y + p(3) );
>> optp = fminsearch( objfun, po, ... );