Evaluating a function with respect to 2 variables

조회 수: 7 (최근 30일)
Larry McNamara
Larry McNamara 2017년 10월 16일
답변: BhaTTa 2024년 10월 25일 5:01
I've haven't used matlab in 10 years and I'm struggling to evaluate a function for "N" odd iterations from 1-5 and 1-100 with respect to 'x'. I'm required to use 100 values for 'x'. My code thus far is below.
% Constants
D = 2; %cm
SigmaA = .1; %cm^-1
SigmaF = .12; %cm^-1
Atilda = 10*pi; %cm
V = .22; %cm/s^6
Scalar0 = 1;
t=1;
N = 1:3:5;
x = linspace(-20,20,100);
phi = zeros(length(x),length(N));
for i = 1:length(x)
for k=1:length(N)
phi(i,k) = (2*Scalar0*cos((N*pi*x)/Atilda))*(cos((N*pi*x)/Atilda))*(exp(-(V*D*((N*pi*x)/Atilda)+V*SigmaA+V*SigmaF)*t));
plot(X,phi(B))
end
end

답변 (1개)

BhaTTa
BhaTTa 2024년 10월 25일 5:01
Hey @Larry McNamara, I assume that you want to evaluate a function for "N" odd iterations from 1 to 5 and 1 to 100 with respect to x, and to plot the results, you'll need to make a below adjustments to your code:
  1. Define the Range of N: You should define N correctly for the two different cases: 1 to 5, and 1 to 100. Since you want odd iterations, you can use 1:2:5 and 1:2:99.
  2. Loop Through Each N: For each value of N, compute the function and store the results.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by