필터 지우기
필터 지우기

using a function to answer new questions

조회 수: 1 (최근 30일)
jacob Mitch
jacob Mitch 2019년 10월 1일
댓글: jacob Mitch 2019년 10월 1일
I have created
function y=Gss(n);
a=3; b=0.2; N(1)=0.2;
for v=2:n+1
N(v)=exp(-a*N(v-1)^2)+b;
end
y=N(v);
How would I use this code to calculate something like
if abs(N(v-1)-N(v))<5
answer=v
else v=v+1 'till you get the desired v'
end
Do I create another script and call Gss(n) or how would I write it in the first function file. I think I would have to use a loop for the second code

채택된 답변

David Hill
David Hill 2019년 10월 1일
function [N,answer] = Gss(n);
N=zeros(1,n+1);
answer=0;
a=3; b=0.2; N(1)=0.2;
for v=2:n+1
N(v)=exp(-a*N(v-1)^2)+b;
if abs(N(v-1)-N(v))<5
answer=v;
end
end
end
Or you could just wait to get array N back and then:
function N = Gss(n);
N=zeros(1,n+1);
a=3; b=0.2; N(1)=0.2;
for v=2:n+1
N(v)=exp(-a*N(v-1)^2)+b;
end
end
%once array N comes back
answer=find(abs(diff(N))<5)+1;%this provides multiple answers if multiple differences are <5
  댓글 수: 1
jacob Mitch
jacob Mitch 2019년 10월 1일
Hi Thanks David this really allows me to understand well, I just wanted to ask am I able to retain the first y=N(v) that I get from inputing n into Gss(n) value say answer1 and then proceed to calculate the second part as the smallest v such that abs(N(v) -N(v-1))<5 say answer2 whilst outputing each value of N(v) in the iteration.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by