Evaluating a function with respect to 2 variables

7 views (last 30 days)
Larry McNamara
Larry McNamara on 16 Oct 2017
Answered: BhaTTa on 25 Oct 2024 at 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

Answers (1)

BhaTTa
BhaTTa on 25 Oct 2024 at 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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!