Some Matlab technical programming problem

  • Context: MATLAB 
  • Thread starter Thread starter kubekas
  • Start date Start date
  • Tags Tags
    Matlab Programming
Click For Summary

Discussion Overview

The discussion revolves around a technical programming problem in MATLAB related to the implementation of functions that generate a series based on an arbitrary variable. Participants are examining issues related to matrix assignments, variable declarations, and function errors.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Amos describes a function that generates a series and encounters an error related to matrix assignments when using a global variable.
  • Some participants suggest that the error may stem from attempting to assign a vector to a single index in a matrix.
  • One participant points out a potential confusion between the global variable `nu` and a mistakenly referenced variable `nu1` in the function.
  • Another participant questions whether the matrix entries should be redefined as vectors to avoid the assignment error.
  • Amos clarifies that the intended structure of `cc` is a vector, not a matrix.

Areas of Agreement / Disagreement

Participants express differing views on the nature of the errors encountered and the appropriate structure for the variables involved. There is no consensus on the best approach to resolve the issues presented.

Contextual Notes

Participants note potential misunderstandings regarding variable declarations and matrix indexing, but do not resolve the underlying issues with the code.

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 address 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 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K