Can some one tell me where I'm going wrong with this code?
I need to write a program that plots 2d graphics of the current (i) against time (t) with time changing from 0:0.001:0.4 seconds.
This is what I have so far:

댓글 수: 1

Stephen23
Stephen23 2015년 1월 20일
Not that you should never use i or j as your loop indices, as these are the names of the inbuilt imaginary unit .

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2015년 1월 21일

1 개 추천

If you haven’t already solved it by now, you need to incorporate ‘t’ into your equation. Note that including it in ‘omega’ will make your code work:
Vm=240; f=50; R=5; L=0.004; C=0.0015; t=0:0.001:0.04;
for k1=1:length(t)
omega=2*pi*f*t(k1);
z(k1)=sqrt(R^2)+(omega*L-(1./(omega*C)).^2);
i(k1)=Vm/z(k1);
end;
plot(t, i)

댓글 수: 2

steve
steve 2015년 1월 21일
You are a legend. It needed some tweaking, but you got me there!
Star Strider
Star Strider 2015년 1월 21일

My pleasure!

I appreciate your compliment.

I remember my own struggles with ESCI 101 (‘Basic Circuits and Systems’).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John
John 2015년 1월 20일

0 개 추천

You must use positive integers to index into an array. z(j) doesn't work, because you have defined j as a number between 0 and .04. Add an index such as
i = 0;
for j ...
i=i+1;
z(i) = ...
...

댓글 수: 6

Stephen23
Stephen23 2015년 1월 20일
Except you should never use i or j as your loop indices, as these are the names of the inbuilt imaginary unit .
John
John 2015년 1월 21일
It is true that i and j are initially defined as sqrt(-1), but imaginary numbers can be defined as any variable you want. And if you don't ever use i or j as an imaginary number, then it doesn't really matter.
John
Bravo, correct,
actually you can use i and j as indices as often as you want, as long as not conflicting, because when using i or j as (-1).^.5 MATLAB already requires a different syntax: to put the imaginary root right after the numeral, without any multiplication operator.
3+i*4
this is 4 times i, plus whatever real value i has, or MATLAB will return 'unknown variable' if i had nothing assigned previously.
On the other side
3+4i
or
3+4j
this is a complex number.
So, John, feel free to use i and j as loop indices, or use other more compact functions than loops.
Cobeldick has a really high credit ranking in this forum but when it comes to complex variables, he really needs to refresh class notes.
To read why I say this just check my answer and his answer to an asin related question:
crack on John
John BG
James Tursa
James Tursa 2016년 8월 26일
편집: James Tursa 2016년 8월 26일
3+i*4
"this is 4 times i, plus whatever real value i has, or MATLAB will return 'unknown variable' if i had nothing assigned previously."
I don't follow your comment. If i was not assigned a value as a variable previously then wouldn't MATLAB just call the toolbox i function?
@James Tursa —
You’re absolutely correct.
It would do exactly that:
z = 3+i*4
z =
3.0000 + 4.0000i
Stephen23
Stephen23 2016년 8월 27일
편집: Stephen23 2016년 8월 27일
@John BG: Apparently I "really needs to refresh class notes."
Okay, lets have a look at my sources of information:
  • The imaginary unit documentation itself currently states: "it is best to avoid using i and j for variable names if you intend to use them in complex arithmetic."
  • The accepted answer written by an TMW employee (TMW, who write MATLAB in case you were wondering), who states regarding avoiding using i and j that "Stephen, I'd say it's a best practice".
  • The official MATLAB documentation Programming Patterns: Some Common MATLAB Programming Pitfalls and How to Avoid Them: "to avoid this problem, either use different variable names or clear the variables...".
  • The many comments by other MATLAB experts, both on this forum and elsewhere, who essentially comment that "using i or j works, but I would not do it myself..."
Can you please tell me exactly what your references are, that are so much better than these official MATLAB references, and comments by expert users and MATLAB employees?
@John: the attitude of "then it doesn't really matter" would get you fired where I work. Standards and best practice are there to make code easier to write, easier to read, easier to debug, easier to fix, easier to integrate with other code. Contrary to what many beginners think, good code practice make your own life easier too!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

태그

질문:

2015년 1월 20일

편집:

2016년 8월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by