I having trouble with functions

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

 採用された回答

Geoff Hayes
Geoff Hayes 2017 年 2 月 13 日
編集済み: Geoff Hayes 2017 年 2 月 13 日

0 投票

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.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

質問済み:

Jim
2017 年 2 月 13 日

編集済み:

2017 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by