First, I need to state that I'm not exactly a math lover or an avid Matlab user. This is college homework that I have been trying to solve for the last 3 hours with no luck. I have googled like crazy, even asked for help via Facebook (where I got replies, but nothing that solved my questions).
This is my first time using this software and I need to graph some things really quick. But no matter how much I read I keep getting syntax errors and the like.
Basically, I have this equation:
4*x^2 + y^2 + z^2 - 8*x + 2*y + 3 = 0
And I'm trying to plot it/graph it. I thought it would be very straightforward but I'm having problems doing it.
Google searches have shown me the functions ezplot() and ezsurf(), but:
When I do ezplot('4*x^2 + y^2 + z^2 - 8*x + 2*y + 3'), I get the following error:
??? Error using ==> char
Cell elements must be character arrays.
Error in ==> ezplot at 158
fmsg = char(f);
And when I do ezsurf('4*x^2 + y^2 + z^2 - 8*x + 2*y + 3')
The expression 4*x^2 + y^2 + z^2 - 8*x + 2*y + 3 must only have 2 symbolic variables
Error in ==> ezgraph3>surfplot at 526
[F,var] = ezfixfun(F,fargs,flabel);
Error in ==> ezgraph3 at 49
[dummy,h] = surfplot(f,domain,surfstyle,cax,Npts,fixdomain,flabel,fargs);
Error in ==> ezsurf at 65
h = ezgraph3('surf',args{:});
Again, this is really my first time using this software. The teacher didn't give us any explanation so I have no idea of what these errors are supposed to mean, let alone solving them. I have no idea of what I'm doing or what I am supposed to do.
I have tried using solve() to solve for 'z' first and then plot the two results I get.
s = solve('4*x^2 + y^2 + z^2 - 8*x + 2*y + 3', 'z')
s =
-(- 4*x^2 + 8*x - y^2 - 2*y - 3)^(1/2)
(- 4*x^2 + 8*x - y^2 - 2*y - 3)^(1/2)
But that's not working either. When I use ezplot() it tells me to use ezsurf, and when I use ezsurf() it tells me about a variable "h" that I have never even declared:
>> ezsurf('-(- 4*x^2 + 8*x - y^2 - 2*y - 3)^(1/2)', '(- 4*x^2 + 8*x - y^2 - 2*y - 3)^(1/2)')
??? Undefined function or variable "h".
Error in ==> ezgraph3 at 66
hh = h;
Error in ==> ezsurf at 65
h = ezgraph3('surf',args{:});
Any help plotting that will be really appreciated. My head is going to explode because people have been telling me this is supposed to be really straightforward.
Thank you!
The following works for me:
ezsurf('(8*x - 4*x^2 - y^2 - 2*y -3)^(1/2)');
Note that ezsurf accepts a function of 2 dimensions (that maps into a 3rd dimension). So you must express the function in the form f(x, y) = blah, where blah is what you want as your input to ezsurf. All I did was re-arrange your function into the form z = blah, and then input blah to ezsurf.
Oh, and two other things, 1) The resulting graph only tells half the story, since the square root of z^2 can be z or minus z (by default, matlab takes the positive square root). 2) The function as above can only be analyzed without complex numbers (such as I've done here) for a very restricted domain of x and y.
Related
I am trying to find the denominator of a given fraction G, but I cannot find a way to use MATLAB's built-in functions without oversimplifying the fraction, and losing important information.
I've tried using MATLAB's built-in commands "numden", "simplify", and "simplifyFraction" but they keep cancelling equal terms from the numerator and denominator. This is normally okay, but for my application, I need to know all values in the denominator which might cause a hole/instability in the function, G.
I've tried looking into the additional constraints within those functions like "IgnoreAnalyticConstraints", but they don't seem to fix the issue. I've simplified my code to isolate my problem below with my current attempts:
syms 's'
G = 2/(s - 1) + 1/(s + 1) - 4/((s - 1)*(s + 1));
[n,d]=numden(G)
G_simp=simplify(G)
G_simpC=simplify(G,'IgnoreAnalyticConstraints',false)
G_simpF=simplifyFraction(G)
Output:
n = 3
d = s + 1
G_simp = 3/(s + 1)
G_simpC = 3/(s + 1)
G_simpF = 3/(s + 1)
Here is an example fraction input:
G = 2/(s - 1) + 1/(s + 1) - 4/((s - 1)*(s + 1))
which simplifies to:
G = 3*(s - 1)/((s - 1)*(s + 1)). <-desired result
I am trying to keep the fraction in this simplified form, but the built-in commands will cancel the (s-1) terms resulting in:
G = 3/(s + 1). <-actual result
You can use the Control System Toolbox:
s= tf([1 0],1);
G = 2/(s - 1) + 1/(s + 1) - 4/((s - 1)*(s + 1))
zpk(G)
The code s= tf([1 0],1); creates a variable s. It contains a Transfer Function object that represents the transfer function f(s)=s. The line
G = 2/(s - 1) + 1/(s + 1) - 4/((s - 1)*(s + 1))
creates a Transfer Function object that contains the corresponding transfer function. And zpk(G) converts this function to the zero/pole/gain form.
The result of the code above is
G =
3 s^3 - 3 s^2 - 3 s + 3
-----------------------
s^4 - 2 s^2 + 1
Continuous-time transfer function.
ans =
3 (s+1) (s-1)^2
---------------
(s+1)^2 (s-1)^2
Continuous-time zero/pole/gain model.
I have a code that produces a vector in MATLAB, for example the following is a three component vector (n=3):
a1_1 - sin((17*a1_1)/60 + a2_1/8 + a3_1/40 - 0.153233)
(15*a1_1)/16 + a2_1/4 + a3_1/32 - sin((17*a1_1)/60 + a2_1/8 + a3_1/40 - 0.0282326)
(3*a1_1)/4 + a2_1/2 + a3_1/8 - sin((17*a1_1)/60 + a2_1/8 + a3_1/40 + 0.846767)
as you can see each component is a non-linear equation. The three component of the vector forms a system of three non-linear equations having it's variables named as a1_1, a1_2and a1_3. I want to solve this system by fsolve.
How do I do that for arbitrary n?
To use fsolve, your function must accept a vector input and return a vector of the same size. In your case you can accomplish this with an anonymous function:
f = #(a)[a(1) - sin(17*a(1)/60 + a(2)/8 + a(3)/40 - 0.153233);...
15*a(1)/16 + a(2)/4 + a(3)/32 - sin(17*a(1)/60 + a(2)/8 + a(3)/40 - 0.0282326);...
3*a(1)/4 + a(2)/2 + a(3)/8 - sin(17*a(1)/60 + a(2)/8 + a(3)/40 + 0.846767)];
n = 3;
a0 = zeros(n,1); % Initial guess
opts = optimoptions('fsolve','Display','iter','TolFun',1e-8);
[a_sol,a_val,exitflag] = fsolve(f,a0,opts)
This returns
a_sol =
-0.002818738864459
-0.687953796565011
9.488284986072076
Of course there may be more than one solution, especially for larger n. You can choose your initial guess to find the others. See the documentation for fsolve and optimoptions for further details on on specifying options.
Did you try using the solve command ?
[y1,...,yN] = solve(eqns,vars) solves the system of equations eqns for the variables vars. The solutions are assigned to the variables y1,...,yN. If you do not specify the variables, solve uses symvar to find the variables to solve for. In this case, the number of variables that symvar finds is equal to the number of output arguments N.
I have an equation denoted as 'a' in the code. I am trying to solve this equation for different values. When I use solve(eq), it does not solve the problem and gives an error. The solution must be in the form 4 by 1, I guess.
syms x y t
a = 3*x^2 + 2*y +7;
b = rand(4,2);
eq = subs(a,{x,y},{b(:,1)*t,b(:,2)*t,})
eq = (114796936454163404704655535315507*t^2)/81129638414606681695789005144064 + (7027663972423957*t)/4503599627370496 + 7
(2049112225923387932688645929427*t^2)/20282409603651670423947251286016 + (730715964868191*t)/4503599627370496 + 7
(8261891359675172983093065745827*t^2)/20282409603651670423947251286016 + (4185582312538139*t)/2251799813685248 + 7
(95262076673210081160325846940403*t^2)/81129638414606681695789005144064 + (1746749665163685*t)/1125899906842624 + 7
solve(eq)
PS: Since I run this code inside a different function, I am trying to save time here by not using loops.
I have the following equation:
Where N^{optim} and K are my variables and M and S are just constants. I want to do some processing to this function like maximizing it, solving it for some values. So I need to write it in terms of the symbolic variables in Matlab. But I don't know how the summation for the symbolic variables. Any help, recommendation, comment is highly appreciated.
If any one knows how to do it in mathematica, or other software that might help as well but I would prefer Matlab and Mathematica.
Thank you.
In[1]:= Binomial[no - m, m]/Binomial[no, m] Sum[Binomial[s, i] Binomial[m, no]^i
(1 - Binomial[m, no])^(s - i), {i, 0, k}]
Out[1]= -(((1 - Binomial[m, no])^(-1 - k + s)*Binomial[-m + no, m]*(-(((1 -
Binomial[m, no])^(-1))^s*(1 - Binomial[m, no])^k) + ((1-Binomial[m, no])^(-1))^s*
(1 - Binomial[m, no])^k*Binomial[m, no] + Binomial[m, no]^(1 + k)*
Binomial[s, 1 + k]*Hypergeometric2F1[1, 1 + k - s, 2 + k, Binomial[m, no]/
(-1 + Binomial[m, no])]))/Binomial[no, m])
Check that result carefully before you depend on it.
I have a function
function y = testf(x,F,phi,M,beta,alpha)
y = -((F+(1 + phi.*cos(2.*pi.*x))).*M.^3.*(cosh((1 + phi.*cos(2.*pi.*x)).*M)+M.*beta.*sinh((1 + phi.*cos(2.*pi.*x)).*M)))./((1 + phi.*cos(2.*pi.*x)).*M.*cosh((1 + phi.*cos(2.*pi.*x)).*M)+(-1+(1 + phi.*cos(2.*pi.*x)).*M.^2.*beta).*sinh((1 + phi.*cos(2.*pi.*x)).*M))- (alpha.*(M.^2.*(F+(1 + phi.*cos(2.*pi.*x))).*(-1+2.*(1 + phi.*cos(2.*pi.*x)).^2.*M.^2+ cosh(2.*(1 + phi.*cos(2.*pi.*x)).*M)-2.*(1 + phi.*cos(2.*pi.*x)).*M.*sinh(2.*(1 + phi.*cos(2.*pi.*x)).*M)))./(8.*((1 + phi.*cos(2.*pi.*x)).*M.*cosh((1 + phi.*cos(2.*pi.*x)).*M)+(-1+(1 + phi.*cos(2.*pi.*x)).*M.^2.*beta).*sinh((1 + phi.*cos(2.*pi.*x)).*M)).^2));
integrating with
q = quad(#(x) testf (x, F, phi,M, beta, alpha), 0, h);
when q = 0 and x,F,phi,M,beta, how do I find alpha and draw the streamline?
It would be great if you gave some numbers, but here is how you would start this. It assumes you are using a version of Matlab that has MuPad as the Symbolic Engine.
First of all, I wouldn't use quad because symbolic expressions will be involved, use int instead.
If I understood you correctly, have the values of x, F, phi, M, beta and you would like to solve for alpha when q = 0
%define the known variables first
syms alpha %defining symbolic object
Now, the following may not work because it's a huge function:
q = int(y,x,0,h)
If it did, all you have to do is solve and evaluate the results for alpha (this may not work as well):
alpha = eval( solve( 'q' , alpha ) )
If the above didn't achieve anything, you might what to look at the 'IgnoreAnalyticConstraints' option.