I want to use a equation which is stored in variable using inline Function but I am getting an error of 'Check for incorrect argument data type or missing argument in call to function 'coeffs'.
'. Here is the code and the output I want. Matlab
syms x;
eq = input('Enter Equation Of Divisor: ','s');
p = inline(eq);
c = coeffs(p);
disp(c);
> Enter Equation: x-4
> [-4,1]
Thanks.
Use the following code:
syms x
eq = input('Enter Equation Of Divisor: ');
c = coeffs(eq);
disp(c)
Related
I am trying to evaluate a function which is an infinite cosine series at some input values.
EDIT: Posting an image to describe what the infinite series looks like
I wrote the following code to describe it in MATLAB.
function func = cosfun_hat(a,i)
syms m x;
assume(m,'integer');
assumeAlso(m > 0);
sum(x) = sqrt(1-a^2)*symsum(sqrt(2)*a^m*cos(i*sym(pi)*x*2^m+1),m,0,Inf);
func(x) = sum(x);
end
I want to evaluate the returned 'function' func to get numerical values for some input range say x_in = 0:0.001:1.
%Trying to evaluate func at x = 2
%In the command window I write
func = cosfun_hat(0.5,2);
func(2)
which returns the symbolic expression:
(2^(1/2)*3^(1/2)*sum((1/2)^m*(exp(- pi*exp(m*log(2))*4*i - i)/2 + exp(pi*exp(m*log(2))*4*i + i)/2), m == 0..Inf))/2
I tried using subs to evaluate the expression:
%In the command window
syms y;
w(y) = func(y);
y = 2;
subs(w);
But that returns the same symbolic expression. I am quite new to symbolic MATLAB.
Thanks!
EDIT Based on the comment by #NickyMattsson I tried
vpa(func(2))
which returns the numerical value of the expression.
However,
vpa(func(0.1)) returns a symbolic expression:
ans =
1.2247448713915890490986420373529*numeric::sum((1/2)^m*(exp(- (pi*exp(m*log(2))*i)/5 - i)/2 + exp((pi*exp(m*log(2))*i)/5 + i)/2), m == 0..Inf)
The same problem with using double(func(0.1)), double doesn't return anything and is stuck.
Figured out a way to do it without using symbolic MATLAB.
function func = cosfun_hat(a,i,x)
m = 0;
sum = zeros(1,length(x));
sum2 = Inf(1,length(x));
while max(sum2-sum) > 1e-16
disp(m);
sum2 = sum;
sum = sum + sqrt(1-a^2)*sqrt(2)*a^m*cos(i*pi*x*2^(m+1));
m = m+1;
end
func = sum;
end
The sum converges inside 100 iterations.
Now if I do,
%In command window
x_in = -2:0.001:2;
f = cosfun_hat(0.6,2,x_in);
plot(x_in,f);
I get the plot:
Thanks everyone for your help!
Use this command
double(func(2))
I am new to Matlab. I am trying to solve a non-linear equation using this inbuilt Matlab function called fzero() but it's not giving me the results.
The main file goes like
A = 5;
B = 6;
C = 10;
eq = equation (A, B, C);
fzero(#(x)eq);
The other function file is:
function eq = equation (A, B, C)
syms x;
eq = A*x.^2 + B*x + C*(asinh(x)) ;
When I run this code, I get the following error:
Error using fzero (line 118)
The input should be either a structure with valid fields or at least two arguments to
FZERO.
Error in main (line 7)
fzero(#(x)eq);
Could someone help me with this?
EDIT:
WHen I specify the check point as 0, it returns me the following error.
Undefined function 'isfinite' for input arguments of type 'sym'.
Error in fzero (line 308)
elseif ~isfinite(fx) || ~isreal(fx)
Error in main (line 7)
fzero(#(x)eq, 0);
There are several mistakes in your code. For a start, fzero is for finding numerical roots of a non-linear equation, it is not for symbolic computations (check the documentation), so get rid of syms x. The correct way to call fzero in your case is a as follows:
A = 5;
B = 6;
C = 10;
eq = #(x) A*x^2 + B*x + C*(asinh(x));
x0 = 0; % or whatever starting point you want to specify
x = fzero(eq,x0)
You need to specify a guess, x0 point
fun = #sin; % function
x0 = 3; % initial point
x = fzero(fun,x0)
I have the following matlab code. 1st line after while gives error. i am trying to make Newtons method to find roots. for that i need derivative f`(p0).
plz guide me what i am doing wrong and how can i get derivative of function f;
I also tried D(f(p0)) but that didn't work, gives error: Undefined function or method 'D' for input arguments of type 'double'.
format long;
f=#(x) cos(x)-x;
p0 = 0.5;
TOLL = 1e-4;
N = 100;
i = 1;
while (i <= N)
p = p0-f(p0)/diff(f(p0)); %Error, returns empty results this produced error
if ( abs(p-p0) < TOLL)
fprintf('Root of given equation is %f\n', p);
return;
end
i=i+1;
p0 = p;
end
fprintf('Method failed after %d iteration\n', i);
The error is because p0 is a scalar, so f(p0) is a scalar. Then taking diff(f(p0)) wont work.
To find the derivative at p0, you could use this definition of the derivative:
f'(p0) = limit as h->0 of (f(x+h)-f(x))/h.
Pick h as some small number (say 1e-3), using (f(p0+h) and f(p0)), you should be able to get an approximation of the derivative of f at p0.
I have been exploring the Matlab symbolic toolbox, in preparation for an upcoming final. However, I seem not to be able to convert a string inputted from the user to a symbolic expression, and then use it for integration.
a = input('Plese enter a = ');
b = input('Please enter b = ');
function1 = input('Please enter the function: ', 's');
syms x eq1
eq1 = sym(function1);
Integral1 = int(eq1 , x, a, b);
Simpson = 1 / 6 * (b - a) * (subs(eq1 , x, a) + 4 * ...
(subs(eq1 , x, (a+b)/2))...
+ subs(eq1 , x, b));
fprintf('The value of the integral is %s \n', Integral1);
fprtinf('The aprroximation with simp is %s \n', Simpson);
Simpson is the integral approximation from simpson's rule. The error I get is something along the lines of "Conversion to 'sym' returned the MuPAD error: Error: unexpected 'identifier'" the line number would be the line
eq1 = sym(function1);
One way to do this is using eval:
function1 = input('Please enter the function: ', 's');
eq1=eval(function1);
But you need to convert the results to doubles before you can display them with fprintf:
fprintf(''The value of the integral is %s \n', double(Integral1));
Starting R2017b, use str2sym
>> syms f(x)
>> function1 = input('Please enter the function: ', 's');
Please enter the function: sin(x)
>> f(x) = str2sym(function1)
f(x) =
sin(x)
See: https://www.mathworks.com/help/symbolic/str2sym.html
I would like to solve numerically an integral than depends on its previous path. Integral 7.14 from here http://www.mathworks.com/matlabcentral/answers/uploaded_files/8998/1.pdf.
With the code below I am getting an error. What is wrong? Am I solving this integral with this code?
"Error using integralCalc/finalInputChecks (line 516) Output of the function must be the same size as the input. If FUN is an array-valued integrand, set the 'ArrayValued' option to true."
Thank you.
z = 0:1/1000:10^-2
dz = z(2)-z(1); %integration step
sigma = 1;
q=0; %Integral value at z=0
for rr = 1:length(z)
fun = #(z) sigma*((10^4-exp(q))/((10^4+exp(q))));
q = integral(fun,0,z(rr))*dz;
end
q
Replace the line :
q = integral(fun,0,z(rr))*dz;
With:
q = integral(fun,0,z(rr),'ArrayValued',true)*dz;