Some Matlab technical programming problem

In summary: Thanks for the reply.In summary, the function finds the coefficients of a cubic equation using its three matrices. The first matrix is used to find the coefficients of the first equation, the second matrix is used to find the coefficients of the second equation, and the third matrix is used to find the coefficients of the third equation. However, when I try to run the function, I get an error message telling me that the function 'gt' is not defined for values of class 'sym.' I'm not sure what this means, but I would appreciate any help you can give me.
  • #1
kubekas
10
0
Hi there every one

I need some help with the following function:

function cc = find_cc(n)
global nu
cc(1)=(-8.*i.*nu1-3.*n.^2-5+4.*i.*nu1.*n-4.*n)./n./(-n-2+4.*i.*nu1);
cc(2)=(3.*n.^2+2.*n+4)./n./(-n-2+4.*i.*nu1);
cc(3)=-(n-1).*(n+1)./n./(-n-2+4.*i.*nu1);
end
where

\nu is abitrary variable. cc(1), cc(2) and cc(3) are entries of a cc(3,1) matrix. Each time I run the above function I get the following message:

"In an assignment A(I) = B, the number of elements in B and
I must be the same"

I guess the problem here is \nu because if I set \nu to 1 the it works. But I want \new to be abbitrary. The above function is called by this function
function vs=reg_series(s)
global nu;
cc1=find_cc(1);
cc2=find_cc(2);
cc3=find_cc(3);
aa0=1;
aa(1)=aa0*cc1(1);
aa(2)=aa0*cc2(2)+aa(1)*cc2(1);
aa(3)=aa0*cc3(3)+aa(1)*cc3(2)+aa(2)*cc3(1);
sn=s.^3;
ss=aa0+aa(1).*s+aa(2).*s.^2+aa(3).*s.^3;
dss_ds=aa(1)+2.*s.*aa(2)+3.*s.^2.*aa(3);
asn=ones(length(s));
nn=4;
while ((abs(nn*asn(length(asn)))>1e-18) && (nn<500))
cct=find_cc(nn);
aa(nn)=aa(nn-1)*cct(1)+aa(nn-2)*cct(2)+aa(nn-3)*cct(3);
dss_ds=dss_ds+sn.*nn*aa(nn);
sn=s.*sn;
asn=aa(nn).*sn;
ss=ss+asn;
nn=nn+1;
end
dss_dz=-2*dss_ds;
vs=ss./dss_dz;
end


to generate a series with \nu as abitrary function. If I use "syms" on to keep \nu as a variables in the function cc = find_cc(n) above, then function vs=reg_series(s) is given me the following error message after the second last end command

"Function 'gt' is not defined for values of class 'sym'."

ANY HELP GUYS?
Amos
 
Physics news on Phys.org
  • #2
Your code is really cumbersome and you can get rid of .* products if you use scalars.

The first error happens when you want to assign a vector to a single index e.g.

A(1) = [3 4]

Second one I don't know becuse there is no 'gt' function appearing in your code.
 
  • #3
Thank you Trambolin for the reply. But are you implying that I should then redefine the matrix entries to be vectors?
 
  • #4
You pass the variable n as an argument of the function and you have the variable nu declared as global, but you use a variable nu1 in your function.
If cc is a matrix, to adress its first line you must write cc(1,:) and not cc(1).
 
  • #5
Sorry CEL, I meant nu not nu1. Also cc is meant to be a vector.
 

1. How do I solve a system of linear equations in Matlab?

To solve a system of linear equations in Matlab, you can use the backslash operator (\) or the "solve" function. The backslash operator is typically faster and more accurate for large systems, while the "solve" function allows for more control over the solution method. Both methods require you to input your system of equations in matrix form.

2. How can I plot a function in Matlab?

To plot a function in Matlab, you can use the "plot" function. First, define the function using the "function" keyword. Then, create a vector of x-values using the "linspace" function. Finally, use the "plot" function to plot the x-values against the function. You can also customize the plot with additional arguments, such as color and line style.

3. What is the difference between a script and a function in Matlab?

A script in Matlab is a file that contains a sequence of commands to be executed in order. It is typically used for repetitive or simple tasks. A function, on the other hand, is a file that contains a sequence of commands that perform a specific task and can take in inputs and return outputs. Functions are useful for creating modular and reusable code.

4. How do I save my workspace variables in Matlab?

To save your workspace variables in Matlab, you can use the "save" function. This will save all variables currently in your workspace to a .mat file. You can also specify which variables to save by using the syntax "save filename var1 var2 ...". The saved variables can then be loaded back into your workspace using the "load" function.

5. How do I debug my code in Matlab?

To debug your code in Matlab, you can use the "dbstop" function to set breakpoints at specific lines in your code. This will pause the execution of your code at those breakpoints, allowing you to inspect variables and step through the code line by line. You can also use the "dbclear" function to remove breakpoints, and the "dbcont" function to continue executing your code after a breakpoint is reached.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
121
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Linear and Abstract Algebra
Replies
2
Views
997
Back
Top