MATLAB symbolic expression - matlab

I have two expressions which are T="x +B " and K="Dt+Dc" and "T=K" . I want to write this expression for just D i.e. "D= (x+B)/t+c". Is there any function for this situation in MATLAB? My real expressions are not short which I gave as example.

Related

Write recursive sub-functions and non-recursive sub-functions in Matlab

I'm newbie to Matlab
I have an assignment :
Legendre polynomial Pn(x), n=0,1,2,. . . The recursive formula of is
Write recursive sub-functions and non-recursive sub-functions separately to find the value of the Legendre polynomial function
This is my code :
function P =Legendre(n,x)
syms x;
n = input('n=');
if n==0
P=1;
elseif n==1
P=x;
elseif n>=2
P=((2*n-1)/n)*x*Legendre(n-1)-((n-1)/n)*Legendre(n-2);
end
end
But I get an error message:
Unrecognized function or variable 'Legendre'.
Error in ti4 (line 9)
P=((2*n-1)/n)*x*Legendre(n-1)-((n-1)/n)*Legendre(n-2);
Sorry for the stupid question. Can anyone help me? Thank u so much
A few things are probably going on here.
File name needs to match function name (for the primary function)
In your case, the filename needs to be Legendre.m.
Symbolic toolbox OR do you want an answer
for most uses of this function, I would leave two full inputs, just as you have them. Bur I would remove the first two lines completely.
As it is, the first two lines will break your inputs. The value for n is reset by the input function. I'm actually not sure what happens when you declare an existing variable x, to a sym.
Input consistency
You are setting up a function with two inputs, named n and x. But when you maek your recursive calls you only pass in one variable. The easiest thing to do here is simply keep passing n in as the first input.
(Right now, you are trying to pass in x in the recursive calls, but it will be interpreted as n.)

"Array indices must be positive integers or logical values"

Problem Details and clues
%For this problem write a script file called NC.m that implements
%the Newton-Cotes method of integration for an arbitrary function f(x). It
%should take as inputs the function and the limits of integration [a: b] and
%output the value of the definite integral. Specifically, you should use the
%Trapezoid rule as presented in Equation (11.73)
function [f]= NC(a,b,fun) %newton-cotes
%a and b are limits of intergration
%setting it up
f(a)= fun(a); %y value for lower limit
f(b)= fun(b); %y value for upper limit
%the actual function
f= (b-a)*(f(a)+f(b))/2;
end
What am i doing wrong? When I type, [f]= NC(-3,0,fun) and set fun= #(x)normpdf(x) . it keeps on returning "Array indices must be positive integers or logical values". Can someone shine some light on this?
The issue is that you try to assign to f(a) where a=0, so you mixed between a vector index and value, as well as use f for two different purposes, one as the output of the function NC, and one for the value of fun(x), that's not a good idea.
Instead you can define the output in a separate variable:
fa=fun(a);
fb=fun(n);
f=(b-a)*(fa+fb)/2;
or just write: f=(b-a)*(fun(a)+fun(b))/2;
The problem lies in the assignments to f(a) and f(b).
The syntax f(x) has three interpretations in MATLAB that are dependent on the type of f:
indexing the array f using the strictly positive integer index (or logical index) x
evaluation of the function handle f using the value of the variable x
manipulation, in some manner, of the symbolic functionf(x) using the symbolic variable x.
Due to MATLAB's dynamic typing and defaulting to double arrays, MATLAB is interpreting the assignments to f(a) and f(b) as the item (1): MATLAB is taking f to be a double array and is expecting a and b to be valid indexes into the array.
Per your intent then, a simple assignment to variable symbols without the parentheses (e.g., fa and fb) should solve your problem.

converting long decimal to scientific notation in symbolic expression Matlab

I know that if I want to convert a numerical expression to scientific notation, I shall use eps(). However, I have a symbolic expression where the results shall be displayed with scientific notation. For example:
the expression is stored in result which is:
result = 0.000002*x^2 + 0.000005*x + 0.000001
But, the desired result is:
2.0e-6*x^2 + 5.0e-6*x + 1.0e-6
When applying
result = eps(result);
I am getting this error:
Error using eps
Class must be 'single' or 'double'.
Notice that I have used syms x; to generate the previous expression.
My experience in Matlab is very shallow. Sorry if this question is so basic.
I have found the solution while searching. The solution is found here.
For short. It is by using vpa(result,k) where k is the number of significant digits.

Matlab symbolic function conversion without dot for matrix operation

When converting symbolic expression to matlabFunction, expression like
x=sym('x')
f=- x^3/6 + x
g=matlabFunction(f)
-> #(x)x-x.^3.*(1.0./6.0)
which is not what I want because x is gonna be a matrix and my application requires actual matrix multiplication such as x^3 instead of the dot product form of x.^3
The only way to get it working is to use anonymous function, i.e.
g=#(x) - x^3/6 + x
->#(x)-x^3/6+x
However, the issue with anonymous function is that I cannot use substitution but to type the entire formulation, i.e.
g=#(x) f
-> #(x)f which shows that expression substitution does not work
In short, I will need to solve either one of the technical difficulties: (1) If I use matlabFunction, how do I remove all the dot after the conversion? or (2) If I use anonymous function, how do I bypass typing the symbolic expression if I have already defined 'f' for the expression?
I am totally lost here and I hope someone familiar with matlab can give me 2 cents.
Thank you!
You can convert the sym object to a string when calculating the anonymous function:
g=#(x)eval(char(f))
Alternatively, you can use the following code
h=eval(['#(x)' char(f)])
instead of matlabFunction

How to replace symbol ' * ' with ' .* ' in matlab expression?

I have a long expression & I need to replace all multiplication symbols ' * ' with element-by-element multiplication symbols ' .* ' in it. It takes too long to do it by hand. Is there an easy way to do it?
Thanks
I think that Ctrl + H is what you look for.
I would like to do two points.
I would do the replacement with a regular expression. like regexprep('\.?*\*','.*') that will take into account if there is already a . before.
The way to do it. The program sed would would be perfect to do it. But you could as well do it in Matlab - loop over all files and replace the text.
Keep in mind that matlab will not be aware of the changes till it rehashes the files. Meaning you can not use the replaced functions immediatly.
To do it programatically you should use a regular expression replacement:
>> str = 'x * y .* z';
>> regexprep(str, '\.?\*', '.*')
ans =
x .* y .* z
The regular expression \.?\* means "match all strings that are 0 or 1 dots (\.?) followed by the multiplication symbol (\*), i.e. it matches the strings '.*' and '*', and replaces both of them with '.*'.
Matlab has a builtin function (I think that it's in all installs) to do this: vectorize. The Symbolic Toolbox uses this function. It will also convert ^ and / to the element-wise equivalents too. Its based on strrep and strfind which are join going to be faster than Matlab's slow regular expression implementation. type edit vectorize in the command window to see the brief code, which you could easily copy and modify for your needs.