I having trouble with functions

7 vues (au cours des 30 derniers jours)
Jim
Jim le 13 Fév 2017
Modifié(e) : Geoff Hayes le 13 Fév 2017
I'm having trouble with my function working with an electrical field problem:
I have my code inserted below, but don't seem to understand how to do the second half of the problem by. Any help would be great. Thank You.
Main Code:
Q = linspace(1,12,12); %sec
r = linspace(0.05,0.6,12); %meter
Q = linspace(1,14,14); %sec
r = linspace(0.76,0.89,14); %meter
Q_t1 = linspace(1.3,1.5,12);
Q_t2 = linspace(1.3,1.7,14); %This is for the second half
[E] = E_sphere(r, Q);
%[E] = E_sphere(r, Q); %This is for the second half
subplot(1,2,1)
plot(E,Q_t1)
subplot(1,2,2)
plot(E,Q_t2)
Function Code:
function [E] = E_sphere(r,Q)
e_o = 8.85e-12; %F/m
E = (Q)/4.*pi.*e_o.*r.*(Q).^2;
end

Réponse acceptée

Geoff Hayes
Geoff Hayes le 13 Fév 2017
Modifié(e) : Geoff Hayes le 13 Fév 2017
Jim - it isn't clear why you are overwriting your r and Q for the first half with that from the second half. Try keeping your local variables distinct as
Q1 = linspace(1.3,1.5,12); %sec
r1 = linspace(0.05,0.6,12); %meter
Q2 = linspace(1.3,1.7,14); %sec
r2 = linspace(0.76,0.89,14); %meter
Then call your function twice as
E1 = E_sphere(r1,Q1);
E2 = E_sphere(r2,Q2);
The inputs to your function do not have to be named r and Q. Likewise, the output does not have to be named E. They can be named however you want but you do want the names to be relevant or specific to their purpose. In this case, I have just added a 1 or 2 to the end of each to indicate that the inputs and results are from data set 1 or 2.
Also, note how the Q's are initialized as per the problem description.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by