MATLAB Some Matlab technical programming problem

AI Thread Summary
The discussion revolves around a MATLAB function designed to compute a matrix based on an arbitrary variable, nu. The user encounters an error message indicating a mismatch in the number of elements during assignment, which is suspected to be linked to the handling of the global variable nu. The function find_cc is called within another function, reg_series, which aims to generate a series based on nu. When attempting to use symbolic variables with "syms," a different error related to the 'gt' function arises, indicating a potential issue with how comparisons are handled in symbolic form. Participants suggest that the user may need to redefine matrix entries as vectors and clarify the use of nu versus nu1. Additionally, it is noted that the assignment of vectors to single indices in MATLAB can lead to errors, and proper indexing of matrices is emphasized.
kubekas
Messages
9
Reaction score
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
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.
 
Thank you Trambolin for the reply. But are you implying that I should then redefine the matrix entries to be vectors?
 
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).
 
Sorry CEL, I meant nu not nu1. Also cc is meant to be a vector.
 

Similar threads

Replies
4
Views
1K
Replies
8
Views
2K
Replies
10
Views
3K
Replies
2
Views
3K
Back
Top