How can I do a graphic using plot and changing the values of x axis from 1 to 500 at intervals of one?

6 views (last 30 days)
Hello, I'm trying to calculate the wavelength, for this I have previously created that function in matlab, and now I have to calculate the wavelength with a loop, which changes the value of the x-axis from 1 to 500 at intervals of one. And make a graph with the plot function. I have tried, but I always get the message "...exceed arrays bounds". I've tried to fix it, but I'm learning Matlab and I need a little bit of help.
That's what I've done:
First, the name of the function I've created: function [longitudonda] = longitudonda(T,H)
And here, you can see the codes where I'm having problems:
H=1:500;
T=10; (this is the period)
for K=1:length(H)
longitudonda(K)=longitudonda(H(K),T);
end
plot(H,longitudonda)
Greetings.

Answers (1)

Walter Roberson
Walter Roberson on 11 Mar 2023
for K=1:length(H)
longitudonda(K)=longitudonda(H(K),T);
end
When K is 1, the right hand side is longitudonda(H(1),10) which calls the two-parameter function longitudonda .
After the function has been called and has successfully returned, the result (hopefully a scalar) is assigned to the scalar output location longitudonda(K) -- a variable named longitudonda
When K is 2, the right hand side is longitudonda(H(2),10) . But now longitudonda is a variable not a function and it does not have as many elements as required to index it at H(2),10 .
The problem arises from using the same output variable name as the name of the function.

Categories

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

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!