Use a MATLAB Workspace to get the unknown values in an AC circuit

In summary: I resolve it again .Plots are attached below.clc;clear all;%partaf=800;xc=1e-6;r=50;t=0:1e-6:5e-3is=2*cos(1600*pi*t);w=2*pi*f;l=xl*j*w;c=1/(xc*j*w);z=l+r;zeq=(z*c)/(c+z);vo=is*zeq; %v=total current*total Zfigure (1)subplot(2,
  • #1
Fatima Hasan
319
14
Homework Statement
The question is attached below.
Relevant Equations
-
The plot of part b is attached below.
I got a warning on part a , I don't know where is my mistake .
Please check my work .
Part A
Matlab:
clc;
clear all;
%parta
f=800;
t=0:10e-6:0.005;
xc=10^(-6);
xl=40e-3;
for i=1:(0.005)/(10e-6)
    w=2*f*pi;
    zeq=xl*j*w+50;
    is(i)=2*(1600*t(i)*pi);
    c=1/(xc*j*w);
    i1(i)=is(i)*c/(c+zeq); %apply CDR
    vo(i)=i1(i)*zeq;
end
figure (1)
subplot(2,1,1) , plot (t,vo)
subplot (2,1,2) , plot (t,is)
%partb
f=10:10:1000;
xl=40e-3;
xc=1e-6;
r=50;
for i=1:100 %(1000-10)/10+1=100
    w(i)=2*f(i)*pi;
    zeq(i)=xl*j*w(i)+r;
    c(i)=1/(xc*j*w(i));
    l(i)=xl*w(i)*j;
    zeq(i)=r+l(i);
    zt(i)=zeq(i)+c(i);
    is(i)=2*(cosd(0)+sind(0)*j); %convert to recantgular form
    vo(i)=(is(i)*c(i)/zt(i))*zeq(i); %Apply CDR to find the current and multiply it by Zeq to get the voltage
end
logscale1=20*log(abs(vo));
logscale2=20*(log(rad2deg(angle(vo))));
subplot(2,1,1) , plot(f,logscale1)
subplot(2,1,2) , plot(f,logscale2)
 

Attachments

  • Q2.PNG
    Q2.PNG
    35.4 KB · Views: 180
  • plotb.PNG
    plotb.PNG
    12.1 KB · Views: 195
Physics news on Phys.org
  • #2
I don't have Matlab, but Octave gives an error in line 11: the index ##i## in array ##is## is invalid because ##i## is not an integer...
 
  • #3
BvU said:
I don't have Matlab, but Octave gives an error in line 11: the index ##i## in array ##is## is invalid because ##i## is not an integer...

That sounds likely just looking at the code. Actually providing the text of the message would be useful, we can't tell much from "I got a warning somewhere in my code".

I know that 0.005/10e-6 looks like it should be an integer, but floating point arithmetic in computers is not exact. Many numbers that are terminating decimals in base 10 are repeating decimals in base 2.

I would try changing the limits of the FOR loop to "for i=1:round(0.005/10e-6)"
 
  • #4
RPinPA said:
That sounds likely just looking at the code. Actually providing the text of the message would be useful, we can't tell much from "I got a warning somewhere in my code".

I know that 0.005/10e-6 looks like it should be an integer, but floating point arithmetic in computers is not exact. Many numbers that are terminating decimals in base 10 are repeating decimals in base 2.

I would try changing the limits of the FOR loop to "for i=1:round(0.005/10e-6)"

I resolve it again .
Plots are attached below.
Matlab:
clc;
clear all;
%parta
f=800;
xl=40e-3;
xc=1e-6;
r=50;
t=0:1e-6:5e-3
is=2*cos(1600*pi*t);
w=2*pi*f;
l=xl*j*w;
c=1/(xc*j*w);
z=l+r;
zeq=(z*c)/(c+z);
vo=is*zeq;  %v=total current*total Z
figure (1)
subplot(2,1,1) , plot (t,vo)
subplot(2,1,2) , plot (t,is)
%part b
f=10:10:1000;
xl=40e-3;
xc=1e-6;
r=50;
for i=1:100 %(1000-10)/10+1=100
    w=2*pi*f(i);
    c=1/(xc*j*w);
    l=xl*w*j;
    z=l+r;
    zeq=(z*c)/(z+c);
    is=2*(cosd(0)+sind(0)*j); %convert to rectangular form
    vo(i)=is*zeq;
    vom(i)=abs(vo(i));
    voa=rad2deg(angle(vo(i)));
end
figure (2)
    semilogx(f,vom)
figure (3)
    semilogx(f,voa)
 

Attachments

  • figure1.PNG
    figure1.PNG
    20.2 KB · Views: 166
  • figure2.PNG
    figure2.PNG
    8.9 KB · Views: 156
  • figure3_.PNG
    figure3_.PNG
    8.3 KB · Views: 167
Last edited:
  • #5
Fatima Hasan said:
voa=rad2deg(angle(vo(i)));
I missed (i) , it should be voa(i)=rad2deg(angle(vo(i)));
 

1. What is a MATLAB Workspace?

A MATLAB Workspace is a collection of variables and their values that are currently being used in a MATLAB session. It is essentially a storage space for data and allows for easy access to variables and their values.

2. How can I use a MATLAB Workspace to get unknown values in an AC circuit?

To use a MATLAB Workspace for an AC circuit, you would first need to define the variables and their values in the Workspace. This would include the known values such as resistance, capacitance, and inductance. Then, you can use MATLAB functions and equations to solve for the unknown values, such as voltage, current, or phase angle.

3. Can I import data from an external source into the MATLAB Workspace?

Yes, you can import data from external sources such as Excel spreadsheets or text files into the MATLAB Workspace. This can be done using the "importdata" function or by manually entering the data into a matrix or array in the Workspace.

4. Is it possible to save the Workspace for future use?

Yes, you can save the Workspace for future use by using the "save" function in MATLAB. This will save all the variables and their values in the Workspace as a .mat file, which can be loaded back into MATLAB in a future session.

5. Can I clear the Workspace and start fresh?

Yes, you can clear the Workspace and start fresh by using the "clear" function in MATLAB. This will remove all variables and their values from the Workspace, allowing you to start with a clean slate for your next project or calculation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
26
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Introductory Physics Homework Help
Replies
3
Views
1K
Back
Top