Matlab: how to manupulate expression - matlab

I am currently having an equation that contains 4~5 variables, and I am unfamiliar with matlab. I want to see how a variable change with the other variable fixed (might assign them some value). Maying graphing them out will help. I found it cumbersome to work in the main GUI in matlab, this is mainly after I type in my equation, I didn't know if the expression is what I want because of missing brackets, etc.
Recently I found mudpad, it is better because it will show you the expression in pretty form. I am wondering is there any other tool-box that is more suitable for my intention?
Update:
Ps=1;
Pr=1;
Pd=1;
g=0.25;
d1=1;
d2=1;
n=0.18;
Yr = #(x)(Ps)/(d1*((Pd*g^2)/d2 + (n*(x- 2))/(x- 1)));
Yr_=ezplot(Yr,0,1);
There is actually 4 more equations to plot and I am only posting one of them.

Related

How to get the zeros of the given equation using fzero in MATLAB?

I have the following function that I wish to solve using fzero:
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)}
Here, C, T_m, T_i, and L_f are all input by the user.
On trying to solve using fzero, MATLAB gives the following error.
Undefined function or variable 'X'.
(where X are the variables stated above)
This error is understandable. But is there a way around it? How do I solve this?
This is answered to the best of my understanding after reading your question as it's not really clear what you are exactly trying and what you want exactly.
Posting the exact lines of code helps a big deal in understanding(as clean as possible, remove clutter). If then the output that matlab gives is added it becomes a whole lot easier to make sure we answer your question properly and it allows us to try it out. Usually it's a good idea to give some example values for data that is to be entered by the user anyway.
First of to make it a function it either needs a handle.
Or if you have it saved it as a matlab file you generally do not want other inputs in your m file then the variable.
So,
function [out]=yourfun(in)
constants=your values; %you can set a input or inputdlg to get a value from the user
out= something something, your lambda thingy probably; %this is the equation/function you're solving for
end
Now since that is not all that convenient I suggest the following
%declare or get your constants here, above the function makes it easier
syms lambda
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)};
hf=matlabFunction(f); %this way matlab automatically converts it to a function handle, alternatively put #(lambda) in front
fzero(hf,x0)
Also this matlab page might help you as well ;)

Creating functions in Matlab

Hi, I am trying to write a function as per the question. I have tried to create four sub-matrices which are the reverse of each other and then multiply to give the products demanded by the question. My attempt:
function T = custom_blocksT(n,m)
T(1:end,end-1:1);
T(1:end,end:-1:1)*2;
T(1:end,end:-1:1)*3;
T(1:end,end:-1:1)*4;
What I'm unsure of is
(i) What do the the indivual sub-matrices(T(1:end,end-1:1);)need to be equal to? I was thinking of(1:3)?
(ii) I tried to create a generic sub-matrix which can take any size matrix input using end was this correct or can't you do that? I keep getting this error
Undefined function or variable 'T'.
Error in custom_blocksT (line 2)
T(1:end,end-1:1);
I have searched the Matlab documentation and stacked overflow, but the problem is I'm not quite sure what I'm supposed to be looking for in terms of solving this question.
If someone could help me I would be very thankfull.
There are many problems with your function:
function T = custom_blocksT(n,m)
T(1:end,end-1:1);
T(1:end,end:-1:1)*2;
T(1:end,end:-1:1)*3;
T(1:end,end:-1:1)*4;
end
This is an extremely basic question, I highly recommend you find and work through some very basic MATLAB tutorials before continuing, even before reading this answer to be honest.
That said here is what you should have done and a bit of what you did wrong:
First, you are getting the error that T dos not exist because it doesn't. The only variables that exist in your function are those that you create in the function or those that are passed in as parameters. You should have passed in T as a parameter, but instead you passed in n and m which you don't use.
In the question, they call the function using the example:
custom_blocks([1:3;3:-1:1])
So you can see that they are only passing in one variable, your function takes two and that's already a problem. The one variable is the matrix, not it's dimensions. And the matrix they are passing in is [1:3;3:-1:1] which if you type in the command line you will see gives you
[1 2 3
3 2 1]
So for your first line to take in one argument which is that matrix it should rather read
function TOut = custom_blocks(TIn)
Now what they are asking you to do is create a matrix, TOut, which is just different multiples of TIn concatenated.
What you've done with say TIn(1:end,end-1:1)*2; is just ask MATLAB to multiple TIn by 2 (that's the only correct bit) but then do nothing with it. Furthermore, indexing the rows by 1:end will do what you want (i.e. request all the rows) but in MATLAB you can actually just use : for that. Indexing the columns by end-1:1 will also call all the columns, but in reverse order. So in effect you are flipping your matrix left-to-right which I'm sure is not what you wanted. So you could have just written TIn(:,:) but since that's just requesting the entire matrix unchanged you could actually just write TIn.
So now to multiply and concatenate (i.e. stick together) you do this
TOut = [TIn, TIn*2; TIn*3, TIn*4]
The [] is like a concatenate operation where , is for horizontal and ; is for vertical concatenation.
Putting it all together:
function TOut = custom_blocks(TIn)
TOut = [TIn, TIn*2; TIn*3, TIn*4];
end

MatLab latex title doesn't work for powers (^)

In MatLab (R2015a), I want to style the title of my plots using latex.
This is working fine for some functions, but not if there's a power in the equation.
The below code works, and shows a formatted title to the right, and an unformatted title to the left.
It shows the warning:
Warning: Error updating Text.
String must have valid interpreter syntax: y = x^2
syms x y
eq = y == x^2;
subplot(1,2,1)
ezplot(eq)
title(latex(eq),'interpreter','latex')
eq = y == x+2;
subplot(1,2,2)
ezplot(eq)
title(latex(eq),'interpreter','latex')
EDIT:
I just found out I can get it to work by appending $ on both sides. But it seems weird that I would have to do this.
So this works:
title(strcat('$',latex(eq),'$'),'interpreter','latex')
Solution
The problem can be solved easily by adding $-signs before and after the generated LaTeX-expression. So you can change your «title-lines» to:
title(['$',latex(eq),'$'],'interpreter','latex')
An alternative is to use strcat as proposed in your question.
Explanation
Since you basically answered the question yourself already, I'm going to explain why it happened. Hopefully after reading this, it's no longer 'weird' behaviour. If you choose to use the LaTeX-interpreter in Matlab, you really get a LaTeX-interpreter. This means that the provided string must be valid LaTeX-syntax.
Using ^ outside a math-environment is considered invalid syntax because it is a reserved character in LaTeX. Some interpreters automatically add the $ before and after in this case, but throw a warning at the same time.
The output of the latex-function in Matlab is provided without the $-signs. This way you can combine outputs and concatenate if needed without creating a mess with $-signs.
To change to the math-environment in LaTeX you can use the already mentioned shortcut $...$. An alternative way is to use \begin{math} your_equation \end{math}. It produces the same result for your equations and can be used here for demonstration purposes. The following line would do the same job, but is a bit longer to write:
title(['\begin{math}',latex(eq),'\end{math}'],'interpreter','latex')
Now, the reason why only one of your equations is displayed correctly, lies in the invalid character ^ in y = x^2. Matlab then chooses interpreter none and therefore displays the string unformatted. The +-sign in y = x + 2 is valid outside a math-environment, so it gets displayed correctly (but is not interpreted in a math-environment).

MATLAB: using a minimum function within symsum

I am trying to run code similar to the following, I replaced the function I had with one much smaller, to provide a minimum working example:
clear
syms k m
n=2;
symsum(symsum(k*m,m,0,min(k,n-k)),k,0,n)
I receive the following error message:
"Error using sym/min (line 86)
Input arguments must be convertible to floating-point numbers."
I think this means that the min function cannot be used with symbolic arguments. However, I was hoping that MATLAB would be substituting in actual numbers through its iterations of k=0:n.
Is there a way to get this to work? Any help much appreciated. So far I the most relevant page I found was here, but I am somewhat hesitant as I find it difficult to understand what this function does.
EDIT following #horchler, I messed around putting it in various places to try and make it work, and this one did:
clear
syms k m
n=2;
symsum(symsum(k*m,m,0,feval(symengine, 'min', k,n-k)),k,0,n)
Because I do not really understand this feval function, I was curious to whether there was a better, perhaps more commonly-used solution. Although it is a different function, there are many pieces online advising against the eval function, for example. I thought perhaps this one may also carry issues.
I agree that Matlab should be able to solve this as you expect, even though the documentation is clear that it won't.
Why the issue occurs
The problem is due the inner symbolic summation, and the min function itself, being evaluated first:
symsum(k*m,m,0,min(k,n-k))
In this case, the input arguments to sym/min are not "convertible to floating-point numbers" as k is a symbolic variable. It is only after you wrap the above in another symbolic summation that k becomes clearly defined and could conceivably be reduced to numbers, but the inner expression has already generated an error so it's too late.
I think that it's a poor choice for sym/min to return an error. Rather, it should just return itself. This is what the sym/int function does when it can't evaluate an integral symbolically or numerically. MuPAD (see below) and Mathematica 10 also do something like this as well for their min functions.
About the workaround
This directly calls a MuPAD's min function. Calling MuPAD functions from Matlab is discussed in more detail in this article from The MathWorks.
If you like, you can wrap it in a function or an anonymous function to make calling it cleaner, e.g.:
symmin = #(x,y)feval(symengine,'min',x,y);
Then, you code would simply be:
syms k m
n = 2;
symsum(symsum(k*m,m,0,symmin(k,n-k)),k,0,n)
If you look at the code for sym/min in the Symbolic Math toolbox (type edit sym/min in your Command Window), you'll see that it's based on a different function: symobj::maxmin. I don't know why it doesn't just call MuPAD's min, other than performance reasons perhaps. You might consider filing a service request with The MathWorks to ask about this issue.

Matlab formula equation editor GUI

How i can execute an equation from GUI?
Example:
How i can do this with various type of equation?
Thanks the answers.
I'm not entirely sure what you mean, but I think you want to grab the equation that a user types in to a text box and turn it in to a function?
Why does your F change from 3 arguments to 4? i.e. F(x,y,y') --> F(1,0,5,-1) in the next line? (The examples of F you gave don't seem to match up with your equation..)
In any case, check out eval(), perhaps that is what you want.
It takes in a matlab command as a string, e.g. 2 + 3, and evalutes it.
So
eval('2+3') % gives 5
eval('f=#(x,y,ydash) y*sqrt(1+ydash^2)') % gives a function f
f(1,5,-2) % gives 11.1803
But in any case, how do you expect the user to type in the square root symbol and the squared symbol? The eval() approach relies on them typing in syntax that matlab will understand.
If you clarify your question a bit more this will be easier.