Effacer les filtres
Effacer les filtres

Give me an example of a calling function.

1 vue (au cours des 30 derniers jours)
Arpita
Arpita le 9 Déc 2023
Modifié(e) : Stephen23 le 9 Déc 2023
program 1
clc
clear all
close all
format longG
f1 = (.01 + .1)/2;
f2 = (2 + 4)/2;
f3 = (1 + 5)/2; % centre frequency in Hz
f4 = (5 + 10)/2;
f5 = (.1+1)/2;
gamma = 0.5; % geometrical factor
Vs = 3.7; % shear wave velocity in km/s
df = xlsread('q.xlsx', 'A11:G14');
r = df(:,1); % epicentral distance
y1 = df(:,3);
y2 = df(:,4); % (5-10) %amplitude at freq f
y3 = df(:,5);
y4 = df(:,6);
y5 = df(:,7);
a1 = log(y1');
a2 = log(y2');
a3 = log(y3');
a4 = log(y4');
a5 = log(y5');
l = [r - gamma*log(r)]'
p1 = polyfit(l, a1, 1);
c1 = p1(1);
p2 = polyfit(l, a2, 1);
c2 = p2(1);
p3 = polyfit(l, a3, 1);
c3 = p3(1);
p4 = polyfit(l, a4, 1);
c4 = p4(1);
p5 = polyfit(l, a5, 1);
c5 = p5(1)
q1 = -(22/7)*f1/(c1*Vs);
q2 = -(22/7)*f2/(c2*Vs);
q3 = -(22/7)*f3/(c3*Vs);
q4 = -(22/7)*f4/(c4*Vs);
q5 = -(22/7)*f5/(c5*Vs);
b1 = log([q1,q3,q3,q4,q5]');
b2 = -log([f1,f3,f3,f4,f5]');
p = polyfit(b2, b1, 1)
n = p(1) %0.799681956
Q1 = q1*f1^n;
Q2 = q2*f2^n;
Q3 = q3*f3^n;
Q4 = q4*f4^n;
Q5 = q5*f5^n;
Q = (Q1 + Q2 + Q3 + Q4 + Q5)/5
program 2: n is obtained from prog 1. instead of copying and pasting the values again and again, i want to print the value of n and use it in the code
clc
clear all
close all
format longG
f = (.1 + 1)/2; % centre frequency in Hz
gamma = 0.5; % geometrical factor
Vs = 3.7; % shear wave velocity in km/s for bhutan
df = xlsread('q.xlsx', 'A11:G14');
r = df(:,1); % epicentral distance
y = df(:,7); %amplitude at freq f
l1 = log(y)
l2 = r - gamma*log(r);
c = -0.000796990116342973
q0 = -(22/7)*f/(c*Vs)
n = 1.14024306744041 % 0.354443081480142
Q = q0*f^n

Réponses (1)

VBBV
VBBV le 9 Déc 2023
Modifié(e) : VBBV le 9 Déc 2023
Put the prog2 code in a function file (which returns the value of n) and call that function in prog1 script file as shown below
% script file for prog 1
clc
clear all
close all
format longG
f = (.1 + 1)/2; % centre frequency in Hz
gamma = 0.5; % geometrical factor
Vs = 3.7; % shear wave velocity in km/s for bhutan
df = xlsread('q.xlsx', 'A11:G14');
r = df(:,1); % epicentral distance
y = df(:,7); %amplitude at freq f
l1 = log(y)
l2 = r - gamma*log(r);
c = -0.000796990116342973
q0 = -(22/7)*f/(c*Vs)
n = prog2() % call the function prog2 () in this prog1
% n = 1.14024306744041 % 0.354443081480142
Q = q0*f^n
function n = prog2() % put this code towards the end in script file
clc
clear all
close all
format longG
f1 = (.01 + .1)/2;
f2 = (2 + 4)/2;
f3 = (1 + 5)/2; % centre frequency in Hz
f4 = (5 + 10)/2;
f5 = (.1+1)/2;
gamma = 0.5; % geometrical factor
Vs = 3.7; % shear wave velocity in km/s
df = xlsread('q.xlsx', 'A11:G14');
r = df(:,1); % epicentral distance
y1 = df(:,3);
y2 = df(:,4); % (5-10) %amplitude at freq f
y3 = df(:,5);
y4 = df(:,6);
y5 = df(:,7);
a1 = log(y1');
a2 = log(y2');
a3 = log(y3');
a4 = log(y4');
a5 = log(y5');
l = [r - gamma*log(r)]'
p1 = polyfit(l, a1, 1);
c1 = p1(1);
p2 = polyfit(l, a2, 1);
c2 = p2(1);
p3 = polyfit(l, a3, 1);
c3 = p3(1);
p4 = polyfit(l, a4, 1);
c4 = p4(1);
p5 = polyfit(l, a5, 1);
c5 = p5(1)
q1 = -(22/7)*f1/(c1*Vs);
q2 = -(22/7)*f2/(c2*Vs);
q3 = -(22/7)*f3/(c3*Vs);
q4 = -(22/7)*f4/(c4*Vs);
q5 = -(22/7)*f5/(c5*Vs);
b1 = log([q1,q3,q3,q4,q5]');
b2 = -log([f1,f3,f3,f4,f5]');
p = polyfit(b2, b1, 1)
n = p(1) %0.799681956
Q1 = q1*f1^n;
Q2 = q2*f2^n;
Q3 = q3*f3^n;
Q4 = q4*f4^n;
Q5 = q5*f5^n;
Q = (Q1 + Q2 + Q3 + Q4 + Q5)/5
end
  2 commentaires
Arpita
Arpita le 9 Déc 2023
thank you but how do i display the value of n and Q in prog 1?
Stephen23
Stephen23 le 9 Déc 2023
Modifié(e) : Stephen23 le 9 Déc 2023
First, get rid of
clc
clear all
close all
particularly at the start of a function. Probably PROG1 should be a function too.
"thank you but how do i display the value of n and Q in prog 1?"
Your function PROG2 calculates Q but then you do nothing with it. If you want to use it in PROG1 then you need to return it as an output argument, e.g.:
function [n,Q] = prog2()
and call it:
[n,Q] = prog2(); % call the function prog2 () in this prog1

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by