2 equations in one function matlab

조회 수: 9 (최근 30일)
Michael
Michael 2021년 1월 18일
댓글: Jon 2021년 1월 18일
Hello,
when i write this code in matlab it only gives me the value for the Area A and not the circumference C. What did I do wrong? Thanks
function [A,C]= myfunction(radius)
radius=2.45;
A=pi*(radius^2);
C=2*pi*radius;
end

채택된 답변

Jon
Jon 2021년 1월 18일
When you called it did you call it with two output arguments? e.g.
radius = 5
[A,C] = myfunction(radius)
Also, you reassign the radius a value of 2.45 inside of the function, so it will not actually use the value of the radius supplied as an argument. I assume that this is not what you intended
  댓글 수: 2
Michael
Michael 2021년 1월 18일
Hello, yes I used two output arguments, [A,C]. I just have these two equations (area and circumference) and I'd like to add them to a function.
When I do this
radius=2.45;
function [A,C]= myfunction(radius)
A=pi*(radius^2);
C=2*pi*radius;
end
it tells me 'Local function name must be different from the script name'. I'm guessing it's because i've put the radius before the function.
Maybe what i'm looking for is not available in matlab?
Jon
Jon 2021년 1월 18일
You have to put this part into a separate file and save it as myfunction.m
function [A,C]= myfunction(radius)
A=pi*(radius^2);
C=2*pi*radius;
end
Then from the command line you can type
>radius = 2.45
>[A,C] = myfunction(radius)

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by